如果用户犯了错误或输入了错误的内容,我试图让用户通过if
语句返回。我可以让他/她重新启动循环,但不只是将一个循环移回,如何将一个循环移回break
杀死循环,我需要它返回
def storepurchasing():
typeofdevice = input("Enter the tye of item you want as the number indicated as an index \n 1: Phone \n 2: Tablet \n\n Your selection: ")
while True:
if typeofdevice == "1":
break
while True:
typeofphone = input("Enter phone you want you want as the number indicated as an index \n 1: BPCM \n 2: BPSH \n 3: RPSS \n 4: RPLL \n 5: YPLS \n 6: YPLL\n\n Your selection: ")
if typeofphone == "1":
pass
elif typeofphone == "2":
pass
elif typeofphone == "3":
pass
elif typeofphone == "4":
pass
elif typeofphone == "5":
pass
elif typeofphone == "6":
pass
elif typeofdevice == "2":
pass
else:
print("please enter a valid input between 1 and 2")
在您的情况下,使用
continue
比使用break
更好地在输入无效的情况下重新启动循环应该用
break
语句替换那些pass
语句。您还可以将所有这些条件检查放在一行中:相关问题 更多 >
编程相关推荐