ValueError: invalid literal for int() with base 10: ''

前端 未结 18 1903
甜味超标
甜味超标 2020-11-22 02:06

I am creating a program that reads a file and if the first line of the file is not blank, it reads the next four lines. Calculations are performed on those lines and then t

18条回答
  •  逝去的感伤
    2020-11-22 03:03

        readings = (infile.readline())
        print readings
        while readings != 0:
            global count
            readings = int(readings)
    

    There's a problem with that code. readings is a new line read from the file - it's a string. Therefore you should not compare it to 0. Further, you can't just convert it to an integer unless you're sure it's indeed one. For example, empty lines will produce errors here (as you've surely found out).

    And why do you need the global count? That's most certainly bad design in Python.

提交回复
热议问题