我想跳过空白行(没有用户输入的值)。我收到了这个错误。
Traceback (most recent call last):
File "candy3.py", line 16, in <module>
main()
File "candy3.py", line 5, in main
num=input()
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
我的代码是:
def main():
tc=input()
d=0
while(tc!=0):
num=input()
i=0
count=0
for i in range (0, num):
a=input()
count=count+a
if (count%num==0):
print 'YES'
else:
print 'NO'
tc=tc-1
main()
答案 0 :(得分:0)
使用raw_input,并手动转换。这也更省钱。有关完整说明,请参阅here。 例如,您可以使用下面的代码跳过任何非整数的内容。
x = None
while not x:
try:
x = int(raw_input())
except ValueError:
print 'Invalid Number'
答案 1 :(得分:0)
您需要采取的行为,请阅读input文档。
输入([提示])
如果存在prompt参数,则将其写入标准输出而不带尾随换行符。然后,该函数从输入中读取一行,将其转换为字符串(剥离尾部换行符),然后返回该行。读取EOF时,会引发EOFError
尝试这样的事情,代码将捕获输入函数产生的可能异常:
if __name__ == "__main__":
tc = input("How many numbers you want:")
d = 0
while(tc != 0):
try:
num = input("Insert number:")
except Exception, e:
print "Error (try again),", str(e)
continue
i = 0
count = 0
for i in range(0, num):
try:
a = input("Insert number to add to your count:")
count = count + a
except Exception, e:
print "Error (count won't be increased),", str(e)
if (count % num == 0):
print 'YES'
else:
print 'NO'
tc = tc - 1