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

时间:2018-11-22 06:40:59

标签: python loops while-loop conditional

发生的一件糟糕的事情是“摇滚”,有时不会产生任何结果。有时我会玩游戏,并且会正常工作,而其他时候循环会结束并且会玩零场游戏。 如果可以的话,请在程序中使用代码,以便我可以了解错误的位置,然后再进行一些调整以提高效率。我认为while循环中嵌套条件的顺序是我在努力的问题吗?请原谅语言。

card_totals = df.groupby('card').sum()['amount'].reset_index().to_dict(orient='list')
card_totals_dict = dict(zip(card_totals['card'], card_totals['amount']))
card_totals_dict

1 个答案:

答案 0 :(得分:1)

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语句)

相关问题