亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

帶有嵌套 if 語句的 while 循環;消除多次打印并退出循環

帶有嵌套 if 語句的 while 循環;消除多次打印并退出循環

MMTTMM 2021-08-24 19:19:53
發生的一件糟糕的事情是“搖滾”有時不會產生任何結果。有時我會玩游戲,它會運行得很好,而其他時候循環會結束并且會玩零場比賽。如果可以的話,請在程序中使用代碼,這樣我就可以了解我的錯誤在哪里,然后我會感謝一些調整以使其有效。我認為 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=0p2_count=0games_played = 0while 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
查看完整描述

1 回答

?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

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循環獲取輸入。所以值p1和p2保持不變。


所以,這現在可以工作了,更正了一些錯誤..(elif第二條和第三條elif語句中的最后一條語句)


查看完整回答
反對 回復 2021-08-24
  • 1 回答
  • 0 關注
  • 301 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號