这是关于验证用户输入的,我正在做一个教程,不明白他为什么定义了以下内容
以下是代码。我不明白的是他定义了3件事。 选择,Acceptable_range和ins_range。
他为什么必须在_range内定义?
def user_choice():
#Variables
# Initial
choice ='thiscanbeanything'
acceptable_range = range(0,10)
within_range = False
#Two condition to check
#Digit or within range = False
while choice.isdigit() == False or within_range==False:
choice = input("Please enter a number (0-10): ")
#Digit check
if choice.isdigit() == False:
print("sorry that is not a digit!")
#Range check
if choice.isdigit() == True:
if int(choice) in acceptable_range:
within_range = True
else:
print("sorry, you are out of acceptable range")
within_range = False
return int(choice)
Below is the code. What i dont understand is in the Initial part he defined 3 things.
choice, acceptable_range and within_range.
why did he have to define within_range?
def user_choice():
#Variables
# Initial
choice ='thiscanbeanything'
acceptable_range = range(0,10)
within_range = False
#Two condition to check
#Digit or within range = False
while choice.isdigit() == False or within_range==False:
choice = input("Please enter a number (0-10): ")
#Digit check
if choice.isdigit() == False:
print("sorry that is not a digit!")
#Range check
if choice.isdigit() == True:
if int(choice) in acceptable_range:
within_range = True
else:
print("sorry, you are out of acceptable range")
within_range = False
return int(choice)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果未定义 incod> insun ,则永远不会在循环时输入
,您需要将其定义为false首先输入以在循环中输入。一旦您进入循环,此值将根据
选择
值定义(使用Input
函数)If
within_range
wasn't defined, you would never enter in thewhile
loop, you need to define it to false first to enter in the loop. Once you're in the loop, this value is defined depending of thechoice
value (with theinput
function)Ins_Range的目的是如果用户没有提供所需的条件,则保持WILE循环函数的运行。如果提供的值不在0-10或非数字之内,则会提示用户再次提供输入。
The purpose of within_range is to keep the while loop function going if the user did not provide a required condition. In the case where if a value provided does not fall within 0-10 or is a non-digit, it will prompt the user to provide an input again.
如果您的选择不在可接受的范围内,则循环希望您再次输入该号码。
如果您的选择位于可接受的范围内,因此循环将结束,因为在代码中,将是正确的。
如果不使用ins_range,则循环将不会结束。
if your choice is not in the acceptable range, the loop wants you to enter the number again.
if your choice is in the acceptable range so the loop will be end because in the code
within_range
will be true.If doesn't use the within_range, the loop will not end.