-3

これを機能させるにはどうすればよいですか?

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: ''
4

2 に答える 2

6
In [104]: long('')
ValueError: invalid literal for long() with base 10: ''

This error is telling you that x is the empty string.

You could be getting this at the end of the file. It can be fixed with:

while True:
    x = f.readline()
    if x == '': break
于 2013-03-19T22:24:45.790 に答える
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 "..."
于 2013-03-19T22:36:54.580 に答える