简体   繁体   English

用户输入未触发while循环条件

[英]While loop condition not triggered by user input

I am working on a little game to work on my Python skills.我正在开发一个小游戏来锻炼我的 Python 技能。 As you can see below, I am stuck on getting the program to continue after the user inputs an erroneous value.正如您在下面看到的,在用户输入错误值后,我一直坚持让程序继续运行。

One of the things I tried is getting rid of the while loop and wrapping the if statement in a recursive function.我尝试的其中一件事是摆脱 while 循环并将 if 语句包装在递归 function 中。 However, I am not sure if this is the right way, and it's kind of above my level at the moment.但是,我不确定这是否是正确的方法,目前它有点超出我的水平。 Any thoughts would be very welcome!任何想法都会非常受欢迎!

print("Greetings, weary wanderer.")
print("Welcome to Freyjaberg. Choose your weapon.")
#user able to select from list, needs input and to check which they pick

weapon_choice = (input("You can choose between three weapons to defeat the beast!"
                   " Press 1 for Axe, 2 for Crossbow, 3 for Sword."))

while True:
    weapon_choice <= '4'
    if weapon_choice=='1':
        print("You chose the Axe of Might")
        break
    elif weapon_choice=='2':
        print("You chose the Sacred Crossbow")
        break
    elif weapon_choice=='3':
        print("You chose the Elven Sword")
        break
    else:
        weapon_choice >= '4'
        print("Wanderer, there is no such weapon. Please choose from the ones available.")
        input("You can choose between three weapons to defeat the beast! "
              "Press 1 for Axe, 2 for Crossbow, 3 for Sword.")

If I enter 1, 2 or 3, it works fine!如果我输入 1、2 或 3,它工作正常!

Greetings, weary wanderer.
Welcome to Freyjaberg. Choose your weapon.
You can choose between three weapons to defeat the beast! Press 1 for Axe, 2 for Crossbow, 3 for Sword.2
You chose the Sacred Crossbow
Now you must enter the first dungeon. Prepare yourself!

Process finished with exit code 0

If I enter 4 or above, it does show the right error message I expect.如果我输入 4 或更高,它会显示我期望的正确错误消息。 So far so good.到目前为止,一切都很好。 Then it prompts me again (as expected).然后它再次提示我(如预期的那样)。 But when I type 1, 2, or 3 now, it doesn't understand and continue.但是当我现在输入 1、2 或 3 时,它不理解并继续。 It just keeps telling me it's wrong.它只是一直告诉我这是错误的。

Greetings, weary wanderer.
Welcome to Freyjaberg. Choose your weapon.
You can choose between three weapons to defeat the beast! Press 1 for Axe, 2 for Crossbow, 3 for Sword.4
Wanderer, there is no such weapon. Please choose from the ones available.
You can choose between three weapons to defeat the beast! Press 1 for Axe, 2 for Crossbow, 3 for Sword.1
Wanderer, there is no such weapon. Please choose from the ones available.
You can choose between three weapons to defeat the beast! Press 1 for Axe, 2 for Crossbow, 3 for Sword.

Take user input in while loop:在 while 循环中获取用户输入:

while True:
    weapon_choice = (input("You can choose between three weapons to defeat the beast!"
               " Press 1 for Axe, 2 for Crossbow, 3 for Sword."))
    try:
        weapon_choice = int(weapon_choice)
    except Exception as e:
        print ("Please enter a valid number!")
        continue
    if weapon_choice==1:
        print("You chose the Axe of Might")
        break
    elif weapon_choice==2:
        print("You chose the Sacred Crossbow")
        break
    elif weapon_choice==3:
        print("You chose the Elven Sword")
        break
    else:
        print("Wanderer, there is no such weapon. Please choose from the ones  available.")

You have to revalue the weapon_of_choice variable inside the while loop, as in weapon_of_choice = input("You can choose between three weapons to defeat the beast, Press 1 for Axe, 2 for Crossbow. 3 for Sword.") .您必须在 while 循环中重新评估武器选择变量的值,如weapon_of_choice weapon_of_choice = input("You can choose between three weapons to defeat the beast, Press 1 for Axe, 2 for Crossbow. 3 for Sword.") The call to input only asks for user input, and returns the input value, you have to decide what to do with it.input的调用只要求用户输入,并返回输入值,你必须决定如何处理它。

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

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