繁体   English   中英

如何在另一个while循环中使用if else语句

[英]how to use an if else statement in another while loop

我是编码新手。 我想尝试编写一个简单的剪刀石头布游戏。 但是我不知道如何结束比赛。

在该程序的最后,如果用户输入错误,我想再次转到end变量。 我尝试使用注释行,但无法正常工作。

player1 = input("What is player 1's name ? ")
player2 = input("What is player 2's name ? ")

player1 = player1.title()
player2 = player2.title()
while True:
    print(player1 + " What do you choose ? rock / paper / scissors : ")
    a = input()

    print(player2 + " What do you choose ? rock / paper / scissors : ")
    b = input()
    if a == "rock" and b == "scissors" :
        print(player1, "won !!!")
    elif a == "scissors" and b == "rock":
        print(player2, "won !!!")

    elif a == "paper" and b == "rock":
        print(player1, "won !!!")
    elif a == "rock" and b == "paper":
        print(player2, "won !!!")

    elif a == "scissors" and b == "paper":
        print(player1, "won !!!")
    elif a == "paper" and b == "scissors":
        print(player2, "won !!!")

    elif a == b:
        print("Its a tie :-(")

    elif a or b != "rock" or "paper" or "scissors":

        print("Wrong input, Try again")
    end = input("Do you want to play again ? yes/no ") == "yes"
    if input == "yes":
        continue
    else:

        print('''

        GAME OVER''')
        break
#    elif input != "yes" or "no":
#        print("Wrong input, Try again. yes or no ?")

如果输入为“ no”,我希望它结束​​游戏;如果输入为“ yes”,则重新启动游戏;如果输入不正确,我希望再次出现提示。

只需检查end的值

if end is True:
    continue
else:
    break

既然,通过将input()与“ yes”进行比较,将end的值设置为布尔值,它将表明用户是否要结束游戏? 另外,您没有初始化输入变量,并且最后的elif条件将始终为true,如注释中所述。

您的代码有一些需要解决的问题,还有一些可以简化的地方。 我对您的程序进行了一些更改,并添加了一些注释来解释这些更改。

player1 = input("What is player 1's name ? ").title() #This uses chaining to streamline code 
player2 = input("What is player 2's name ? ").title() #Same as above

while True:
    a = input(player1 + " What do you choose ? rock / paper / scissors : ") #no need to use a separate print statement
    b = input(player2 + " What do you choose ? rock / paper / scissors : ")

    valid_entries = ["rock", "paper", "scissors"] #To check for valid inputs

    if (a not in valid_entries) or (b not in valid_entries):
        print("Wrong input, try again")
        continue

    a_number = valid_entries.index(a) #Converting it to numbers for easier comparison
    b_number = valid_entries.index(b)

    if(a_number == b_number):
         print("Its a tie :-(")
    else:
        a_wins = ((a_number > b_number or (b_number == 2 and a_number == 0)) and not (a_number == 2 and b_number == 0)) #uses some number comparisons to see who wins instead of multiple if/elif checks

        if(a_wins):
            print(player1, "won !!!")
        else:
            print(player2, "won !!!")

    end = input("Do you want to play again ? yes/no ")

    while (end !="yes") and (end != "no"):
        print("invalid input, try again")
        end = input("Do you want to play again ? yes/no ")

    if end == "yes":
        continue
    else:
        print("GAME OVER")
        break

这些更改还通过使用另一个while循环进行检查,以查看重新启动游戏的输入是否有效

*请注意,我尚未测试这些更改,因此可能需要进行一些修改

好吧,您可以使用列表简化代码,然后简化if测试。 您可以检查选项的顺序并根据其做出决定。 您也可以使测试成为标准,以最大程度地减少if语句的数量。 这是我改善您的代码的建议。 希望对您有所帮助:

# get playe names
player1 = input("What is player 1's name ? ")
player2 = input("What is player 2's name ? ")
player1 = player1.title()
player2 = player2.title()

# init vars
options = ["rock", "paper", "scissors"]
players = [player1, player2]

# start game
while True:
    a = input(player1 + " What do you choose ? rock / paper / scissors : ")
    b = input(player2 + " What do you choose ? rock / paper / scissors : ")

    # check if inputs are correct
    while (a not in options or b not in options):
        print("Wrong input, Try again")
        a = input(player1 + " What do you choose ? rock / paper / scissors : ")
        b = input(player2 + " What do you choose ? rock / paper / scissors : ")

    # check who won
    if abs(options.index(a) - options.index(b)) == 1:
        print(players[1*int(options.index(a) > options.index(b))], "won !!!")

    elif abs(options.index(b) - options.index(a)) > 1:
        print(players[1*int(options.index(a) > options.index(b))], "won !!!")

    elif a == b:
        print("Its a tie :-(")

    # continue or drop game
    end = input("Do you want to play again ? yes/no ")
    if end == "yes":
        continue
    else:

        print('''

        GAME OVER''')
        break

暂无
暂无

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

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