带有嵌套 if 语句的 While 循环;消除多次打印并退出循环

While loops with nested if statements; Eliminating multiple prints and exiting the loop

发生的一件糟糕的事情是 "rock" 有时不会产生任何结果。有时我会玩这个游戏,它会运行得很好,而其他时候循环会结束并且会玩零个游戏。 如果可以的话,请在程序中使用代码,这样我就可以了解我的错误所在,然后我将不胜感激进行一些调整以使其高效。我认为 while 循环中嵌套条件的顺序是我正在努力解决的问题?语言请见谅

"""Rock, Paper, Scissors Exercise 8"""
game= input("Are you ready to ply? Y or N: ").capitalize()
user1 = input("What's your name? ")
user2 = input("What's your name? ")
p1 = input(user1 + ": Rock, Paper, Scissors? ").lower()
p2 = input(user2 + ": Rock, Paper, Scissors? ").lower()
p1_count=0
p2_count=0
games_played = 0

while game == "Y":
    if p1 == "rock":
        if p2 == "rock":
            print("It\'s a tie!")
            game = input("Are you ready to ply? Y or N: ").capitalize()
            p1_count += 1
            p2_count += 1
            games_played += 1
        elif p2 == "scissors":
            print(user2 + ", you got beat mothafucka!")
            game = input("Are you ready to play? Y or N: ").capitalize()
            p1_count += 1
            games_played += 1
        elif p2 == "paper":
            print(user1 + ", you got beat mothafucka!")
            game = input("Are you ready to play? Y or N: ").capitalize()
            p2_count += 1
            games_played += 1
    elif p1 == "scissors":
        if p2 == "scissors":
            print("It\'s a tie!")
            game = input("Are you ready to play? Y or N: ").capitalize()
            p1_count += 1
            p2_count += 1
            games_played += 1
        elif p2 == "paper":
            print(user2 + ", you got beat mothafucka!")
            game = input("Are you ready to play? Y or N: ").capitalize()
            p1_count += 1
            games_played += 1
        elif p2 == "rock":
            print(user1 + ", you got beat mothafucka!")
            game = input("Are you ready to play? Y or N: ").capitalize()
            p1_count += 1
            games_played += 1
    elif p1 == "paper":
        if p2 == "paper":
            print("It\'s a tie!")
            game = input("Are you ready to ply? Y or N: ").capitalize()
            p1_count += 1
            games_played += 1
        elif p2 == "rock":
            print(user2 + ", you got beat mothafucka!")
            game = input("Are you ready to ply? Y or N: ").capitalize()
            p1_count += 1
            games_played += 1
        elif p2 == "scissors":
            print(user1 + ", you got beat mothafucka!")
            game = input("Are you ready to ply? Y or N: ").capitalize()
            p1_count += 1
            games_played += 1


print("Thank you " + user1 + " and " + user2 + " for playing this classic fucking 
game!")
print("With " + str(games_played) + " games played, " + "the score was " + user1 + " 
with " + str(p1_count) + " and " + user2 + " with " + str(p2_count))
game= input("Are you ready to ply? Y or N: ").capitalize()
user1 = input("What's your name? ")
user2 = input("What's your name? ")

p1_count=0
p2_count=0
games_played = 0

while game == "Y":
    p1 = input(user1 + ": Rock, Paper, Scissors? ").lower()
    p2 = input(user2 + ": Rock, Paper, Scissors? ").lower()
    if p1 == "rock":
        if p2 == "rock":
            print("It\'s a tie!")
            game = input("Are you ready to ply? Y or N: ").capitalize()
            p1_count += 1
            p2_count += 1
            games_played += 1
        elif p2 == "scissors":
            print(user2 + ", you got beat mothafucka!")
            game = input("Are you ready to play? Y or N: ").capitalize()
            p1_count += 1
            games_played += 1
        elif p2 == "paper":
            print(user1 + ", you got beat mothafucka!")
            game = input("Are you ready to play? Y or N: ").capitalize()
            p2_count += 1
            games_played += 1
    elif p1 == "scissors":
        if p2 == "scissors":
            print("It\'s a tie!")
            game = input("Are you ready to play? Y or N: ").capitalize()
            p1_count += 1
            p2_count += 1
            games_played += 1
        elif p2 == "paper":
            print(user2 + ", you got beat mothafucka!")
            game = input("Are you ready to play? Y or N: ").capitalize()
            p1_count += 1
            games_played += 1
        elif p2 == "rock":
            print(user1 + ", you got beat mothafucka!")
            game = input("Are you ready to play? Y or N: ").capitalize()
            p2_count += 1
            games_played += 1
    elif p1 == "paper":
        if p2 == "paper":
            print("It\'s a tie!")
            game = input("Are you ready to ply? Y or N: ").capitalize()
            p1_count += 1
            games_played += 1
        elif p2 == "rock":
            print(user2 + ", you got beat mothafucka!")
            game = input("Are you ready to ply? Y or N: ").capitalize()
            p1_count += 1
            games_played += 1
        elif p2 == "scissors":
            print(user1 + ", you got beat mothafucka!")
            game = input("Are you ready to ply? Y or N: ").capitalize()
            p2_count += 1
            games_played += 1


print("Thank you " + user1 + " and " + user2 + " for playing this classic fucking game!")
print("With " + str(games_played) + " games played, " + "the score was " + user1 + " with " + str(p1_count) + " and " + user2 + " with " + str(p2_count))

只需将这两行 (p1 and p2) 放入 while 循环中,就大功告成了!

这里发生的事情是,您没有为下一次执行 while 循环获取输入。所以 p1p2 的值保持不变。

所以,这现在可以工作了,更正了一些错误..(第二个和第三个 elif 语句中的最后一个 elif 语句)