我正在尝试创建一个循环,其中用户被给予选择1-8,如果他们没有选择1-8,它将他们循环回到重新输入数字1-8。我正在尝试使用一个带有两个条件的while循环。我遗漏了什么?
fm_select = int(input("Enter a number 1-8"))
while fm_select <= 8 and fm_select >= 1:
发布于 2019-11-30 02:38:25
你的范围错了。您希望while
循环在时失败,因为您正在尝试中断该循环。因此,您希望您的循环检查不是1到8之间的的每个数字。相反,你应该这样做
fm_select = 0
while (fm_select < 1 or fm_select > 8):
fm_select = int(input("Enter a number between one and eight: "))
只要他们的输入小于1或大于8,就继续询问
发布于 2019-11-30 02:39:31
像这样的东西应该是可行的
while(True):
fm_select = int(input("Enter a number 1-8"))
if 0 < fm_select < 8:
break
print("try again")
print("you have entered %d" %(fm_select) )
https://stackoverflow.com/questions/59109604
复制相似问题