[英]Python While loop - nesting if statement in for loop to check numbers in array
Really struggling with this at the moment and need some help as to where I am going wrong with this all.目前真的在为此苦苦挣扎,需要一些帮助来解决我在这一切上哪里出了问题。
So currently just doing some practice work and running into an error.所以目前只是做一些练习工作并遇到错误。 Seeing if numbers that a user enters into 'user input' within an array are bigger than a set integer.查看用户在数组中输入“用户输入”的数字是否大于一组 integer。 I want to iterate over the number in the array then have an if statement print out a message saying the number they entered is too high.我想遍历数组中的数字,然后让 if 语句打印出一条消息,说他们输入的数字太高。 I then want the loop to start back over.然后我希望循环重新开始。 The error I am getting is that the loop prints the message but does not iterate back over the while loop and instead just exits.我得到的错误是循环打印消息,但不会在 while 循环上迭代,而只是退出。 I do not know why this is happening and can't figure it out.我不知道为什么会这样,也无法弄清楚。 Any help would be much appreciated, Thank you in advance.任何帮助将不胜感激,在此先感谢您。
user_lottery_numbers = []
while type(user_lottery_numbers) is not int:
try:
user_lottery_numbers = [int(x) for x in input("Please enter your numbers followed by the enter key:").strip(",").split(",")]
for x in user_lottery_numbers:
if (x) > 60:
print ("The number %s you have entered is too high please stay within the range" % (x))
continue
if len(user_lottery_numbers) <6 or 0 in user_lottery_numbers:
print ("Not enough numbers and or you have made one number zero by mistake")
continue
if len(user_lottery_numbers) >6:
print ("Too many numbers")
continue
print (user_lottery_numbers)
print ("Thank you for the correct format of numbers")
break
except ValueError:
print ("You must only enter 6 integers followed by the enter key")
First of all, A break
statement terminates the loop containing it, therefore the break
statement at the end of your while loop causes your loop to terminate right after the for
loop ends.首先, break
语句会终止包含它的循环,因此 while 循环末尾的break
语句会导致循环在for
循环结束后立即终止。 So, if you remove the break
statement you will get the desired outcome:因此,如果您删除break
语句,您将获得所需的结果:
Please enter your numbers followed by the enter key:5,4,61,77
The number 61 you have entered is too high please stay within the range
The number 77 you have entered is too high please stay within the range
Please enter your numbers followed by the enter key:
Secondly, looking at your code, I realize you thought the continue
statement will cause the break
statement to be skipped, however, the continue
statement and the break
statement are not in the same loop.其次,查看您的代码,我意识到您认为continue
语句会导致break
语句被跳过,但是continue
语句和break
语句不在同一个循环中。 You can remove the continue
as it doesn't have any effect here.您可以删除continue
因为它在这里没有任何效果。
If you want the while
loop to finish if the user enters correct input, I would change the code to:如果您希望在用户输入正确输入的情况下完成while
循环,我会将代码更改为:
user_lottery_numbers = []
continue_running = True
bad_input = False
while continue_running:
try:
user_lottery_numbers = [int(x) for x in input("Please enter your
numbers followed by the enter key:").strip(",").split(",")]
for x in user_lottery_numbers:
if x > 60:
print ("The number %s you have entered is too high please
stay within the range" % (x))
bad_input = True
if bad_input:
continue_running = True
else:
continue_running = False
except ValueError:
print ("You must only enter 6 integers followed by the enter key")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.