ValueError:无效的长整型字符:
我该怎么才能让这个工作呢?
n = 1234
f = open("file", "r")
while True:
x=f.readline()
print "*********************"
print n%(long(x))
if n%(long(x))==0:
print x
else:
print "..."
我在学习Python,但遇到了一个我看不懂的错误。我到底哪里做错了呢?
ValueError: invalid literal for long() with base 10: ''
2 个回答
0
一个 try/except
代码块可以是调试这类问题的一个很方便的方法。
n = 1234
f = open("file", "r")
while True:
x=f.readline()
print "*********************"
try: # Add these 3 lines
print n%(long(x))
except ValueError: # to help work out
print "Something went wrong {!r}".format(x) # the problem value
if n%(long(x))==0:
print x
else:
print "..."
6
In [104]: long('')
ValueError: invalid literal for long() with base 10: ''
这个错误是在告诉你,x
是一个空字符串。
你可能是在文件的末尾遇到这个问题。
可以通过以下方式来解决:
while True:
x = f.readline()
if x == '': break