Python While循环中的输入函数
在这个程序中,我试图使while循环中的变量integer_enter一直被赋值为整数,直到在输入中输入0为止。每次运行代码时,“integer\u enter=int(input())”行上都会出现EOF错误。为什么会这样?在while循环中定义一个变量并没有什么错,为什么会出现这样的错误呢 参考代码:Python While循环中的输入函数,python,function,input,while-loop,Python,Function,Input,While Loop,在这个程序中,我试图使while循环中的变量integer_enter一直被赋值为整数,直到在输入中输入0为止。每次运行代码时,“integer\u enter=int(input())”行上都会出现EOF错误。为什么会这样?在while循环中定义一个变量并没有什么错,为什么会出现这样的错误呢 参考代码: list_num = [ ] count_even = 0 loop_condition= True while(loop_condition == True): integer_
list_num = [ ]
count_even = 0
loop_condition= True
while(loop_condition == True):
integer_enter= int(input())
integer_append= list_num.append(integer_enter)
if(integer_enter % 2 == 0):
count_even += 1
elif(integer_enter == 0):
loop_condition = False
print('The number of even integers is %d' % count_even)
print(list_num)
您需要修改循环终止的条件。当输入的
整数为0
时,需要确保未执行if
。是指向代码的工作链接
list_num = [ ]
count_even = 0
loop_condition= True
while(loop_condition == True):
integer_enter= int(input())
integer_append= list_num.append(integer_enter)
if(integer_enter % 2 == 0 and integer_enter != 0):
count_even += 1
elif(integer_enter == 0):
loop_condition = False
print('The number of even integers is %d' % count_even)
print(list_num)
while循环永远不会停止,因为只有当输入的整数等于正数时,0%2==0
才会停止,对吗?顺便说一句,integer\u append为None
将始终为真,因为list.append
不会返回任何内容。谢谢。我忘了0%2也等于0。