python 如何编写while循环来检查两个单独的用户输入?

tmb3ates  于 2023-05-21  发布在  Python
关注(0)|答案(1)|浏览(95)

Python初学者在这里,对不起,如果这是一个简单的问题!
我已经有一个工作while循环了

while True:
do_youwanttoplay = input("Do you want to play? ").lower() 

if do_youwanttoplay == "yes": 
    print ("You are starting with", health, "health")
    print ("Let's play!")
    break
elif do_youwanttoplay == "no":
    print ("Cya...")
    break
else:
    print ("Please enter either yes or no")

但在下一段代码中,我希望它能检查用户是否输入了“yes”或“no”(它目前正在这样做),并检查用户是否输入了“across”或“around

while True:    
    first_choice = input("First choice... Left or Right (left/right)? ").lower() 

    if first_choice == "left":
            ans = input("Nice, you follow the path and reach a lake...Do you swim across or go around (across/around)? ")
            break
    elif first_choice == "right":
            print("test")
    else:
        print ("Please enter either left or right")          

    if ans == "across":
                print ("You managed to get across, but were bit by a fish and lost 5 health")
                health -= 5
                print ("Your health is now", health)
    elif ans == "around": 
                print ("You went around and avoided getting bit by the fish, well done!")
jdzmm42g

jdzmm42g1#

这是一个如何组织while循环的问题。
我能够得到以下工作,在一种方式,我相信是你想要的。
我添加了一个stop布尔值来确定是否应该退出主循环。

health = 150
stop = False
while not stop:
    while True:
        do_youwanttoplay = input("Do you want to play? ").lower()
        if do_youwanttoplay == "yes":
            print("You are starting with", health, "health")
            print("Let's play!")
            break;
        elif do_youwanttoplay == "no":
            print("Cya...")
            stop = True
            break
        else:
            print("Please enter either yes or no")

    if stop:
        break

    while True:
        first_choice = input("First choice... Left or Right (left/right)? ").lower()
        if first_choice == "left":
            print("Nice, you follow the path and reach a lake...")
            ans = input("Do you swim across or go around (across/around)? ")
            if ans == "across":
                print("You managed to get across, but were bit by a fish and lost 5 health")
                health -= 5
                print("Your health is now", health)
            elif ans == "around":
                print("You went around and avoided getting bit by the fish, well done!")
        elif first_choice == "right":
            print("test")
        else:
            print("Please enter either left or right")

相关问题