简体   繁体   English

虽然循环没有返回它的条件

[英]While loop isn't returning its condition

attempts=0

number=int(input("Enter a number from a range of 0 to a 100: "))

while number<0 and number>100:
    attempts=attempts+1
    number=int(input("Number is invalid. Please enter a number from a range of 0 to a 100: "))

attempts=attempts+1    
print("Number of attempts for inputting a valid number: ", attempts)

I'm writing a program where an inputted number is validated from the range 0 to 100. If it's invalid, the number will be rejected and a prompt will be shown to re-input a number.我正在编写一个程序,其中输入的数字在 0 到 100 的范围内进行验证。如果它无效,则该数字将被拒绝,并会显示提示以重新输入数字。 I then need to output the number of attempts it took to input a valid number.然后我需要 output 尝试输入有效数字的次数。

So far, when an integer greater than 0 and less than 100 is inputted, the program works fine.至此,当输入大于 0 小于 100 的 integer 时,程序运行正常。 If an integer, let's say -50 is inputted, the prompt should be shown and it then needs to re-input the number - but my while loop isn't returning its condition and it goes to show an output of:如果输入 integer,假设输入 -50,则应显示提示,然后需要重新输入数字 - 但我的 while 循环没有返回其条件,它会显示 output:

Enter a number from a range of 0 to a 100: -50 
Number of attempts for inputting a valid number:  1

I think in while loop, the condition should be or not and .我认为在while循环中,条件应该是or不是and

attempts=0

number=int(input("Enter a number from a range of 0 to a 100: "))

while number<0 or number>100:
    attempts=attempts+1
    number=int(input("Number is invalid. Please enter a number from a range of 0 to a 100: "))

attempts=attempts+1    
print("Number of attempts for inputting a valid number: ", attempts)

You should use "or" instand of "and":您应该使用“或”来代替“和”:

attempts=0

number=int(input("Enter a number from a range of 0 to a 100: "))

while number<0 or number>100:
    attempts=attempts+1
    number=int(input("Number is invalid. Please enter a number from a range of 0 to a 100: "))

attempts=attempts+1
print("Number of attempts for inputting a valid number: ", attempts)

This is my suggestion:这是我的建议:

attempts=0

number=int(input("Enter a number from a range of 0 to a 100: "))

while number not in range(0, 101):
    attempts=attempts+1
    number=int(input("Number is invalid. Please enter a number from a range of 0 to a 100: "))

attempts=attempts+1    
print("Number of attempts for inputting a valid number: ", attempts)

Result:结果:

Enter a number from a range of 0 to a 100: -1
Number is invalid. Please enter a number from a range of 0 to a 100: 101
Number is invalid. Please enter a number from a range of 0 to a 100: 100
Number of attempts for inputting a valid number:  3
Enter a number from a range of 0 to a 100: 0
Number of attempts for inputting a valid number:  1

The mistake is here while number<0 and number>100: or means at least one condition is correct but and means all the conditions are correct.错误在这里,而number<0 and number>100: or意味着至少一个条件是正确的但意味着所有条件都是正确的。 Now look at the condition No number can fulfill your condition like less than 0 and greater than 100 at a sametime.现在看看条件 没有数字可以同时满足您的条件,例如less than 0 and greater than 100 at a sametime. so you can use or operation so one of the conditions are meet the requirements then code execute.因此您可以使用或操作,因此条件之一满足要求然后执行代码。

attempts=0
invalid_attempts=0
number=int(input("Enter a number from a range of 0 to a 100: "))
while number<0 or number>100:
    
    number=int(input("Number is invalid. Please enter a number from a range of 0 to a 100: "))
    invalid_attempts+=1
attempts=attempts+1    
print("Number of attempts for inputting a valid number: ", attempts)
print("Number of attempts for inputting a Invalid number: ", invalid_attempts)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM