Loops python中if语句的while循环
我正在制作一个小程序,是python的新手,因此有一点理解上的问题,需要知道我的错误,当用户输入了正确的选择(如1、2或3)时,它应该进入循环并运行if语句。一旦完成,它应该退出。但显然,这并不完美Loops python中if语句的while循环,loops,Loops,我正在制作一个小程序,是python的新手,因此有一点理解上的问题,需要知道我的错误,当用户输入了正确的选择(如1、2或3)时,它应该进入循环并运行if语句。一旦完成,它应该退出。但显然,这并不完美 print ("Selection of the mode\n Easy \n Moderate \n tough") selection = int(input("Please enter the right Choice!! > ")) while selection != '':
print ("Selection of the mode\n Easy \n Moderate \n tough")
selection = int(input("Please enter the right Choice!! > "))
while selection != '':
if selection == 1:
print("\ngoodluck")
elif selection == 2:
print('\nyou are brave')
elif selection == 3:
print ('\nyou must be kidding')
selection = int(input("Please enter the right Choice!!! > "))
我现在正在执行此操作,但已完成所有if语句,但我只想在if语句的第一个选择正确后退出。尝试此操作
print ("Selection of the mode\n Easy \n Moderate \n tough")
selection = input("Please enter the right Choice!! > ")
while selection != '':
if selection == '1':
print("\ngoodluck")
break
elif selection == '2':
print('\nyou are brave')
break
elif selection == '3':
print ('\nyou must be kidding')
break
selection = input("Please enter the right Choice!!! > ")
此外,不需要多次使用input()
print ("Selection of the mode\n Easy \n Moderate \n tough")
selection = int(input("Please enter the right Choice!! > "))
while selection != '':
if selection == 1:
print("\ngoodluck")
break
elif selection == 2:
print('\nyou are brave')
break
elif selection == 3:
print ('\nyou must be kidding')
break
selection = input("Please enter the right Choice!!! > ")
您需要在所有打印函数之后添加
break
。@AvinashRaj没有这样做:/break
语句应该像AvinashRaj说的那样工作。@Kutttayqaratera您是说,如果输入1,它不会退出吗?好的@itzmeontv,下面的答案是正确的,但是当键入1、2或3时,while循环仍然没有退出,它仍然在重复出现。
print ("Selection of the mode\n Easy \n Moderate \n tough")
while True:
selection = int(input("Please enter the right Choice!!! > "))
if selection == 1:
print("\ngoodluck")
break
elif selection == 2:
print('\nyou are brave')
break
elif selection == 3:
print ('\nyou must be kidding')
break