程序必须向用户询问均匀的整数最大六次,如果没有结束
该程序向用户询问一个整数。如果他要求6次,该程序结束了。如果数字是,它将返回它。到目前为止,我的代码:
i = 0
for i in range(6):
num = int(input("Num: "))
if num % 2 == 0:
print(num)
else:
num = int(input("Num: "))
i+=1
if i == 6:
break
我的程序在用户给出6个什至没有数字后没有结束,我该如何解决?
The program ask the user for an even integer. If he asks for it 6 times, the program ends. If the number is even, it returns it. My code so far:
i = 0
for i in range(6):
num = int(input("Num: "))
if num % 2 == 0:
print(num)
else:
num = int(input("Num: "))
i+=1
if i == 6:
break
My program doesn't end after user gives 6 not even numbers, how can i fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无需检查
i == 6
自己,这是由循环自动完成的。但是,当输入偶数时,您需要脱离循环。
else:如果循环得出其正常结论,而不是由于
break
而退出,则执行循环的块。You don't need to check for
i == 6
yourself, this is done automatically by thefor
loop.But you need to break out of the loop when an even number is entered.
The
else:
block of a loop is executed if the loop reaches its normal conclusion instead of exiting due tobreak
.如果没有所有额外的线条,这将完成您想要的事情。
This will do what you want, without all the extra lines you had.