第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

我的石頭剪刀布游戲中出現(xiàn)錯誤,在 pass 語句后循環(huán)無法正常工作

我的石頭剪刀布游戲中出現(xiàn)錯誤,在 pass 語句后循環(huán)無法正常工作

紅糖糍粑 2023-12-20 19:51:33
    # Created By Evan Ryan# we import our random module and time modulefrom random import *import time# this will track our winscomp_wins = 0player_wins = 0# this will let us know when the match is finishedendgame = 0# here we use simple print and sleep function to display a welcome messageprint("Welcome to Rock, Paper, Scissors, Lizard, Spock")time.sleep(3)print("This match will be best 3 out of 5")time.sleep(3)print("Good Luck")time.sleep(1)# this will be our function to allow the user to make a choice# they have 5 choices# if they make a typo or choose a non option they will be asked to choose again# we will also convert their selection to a lowercase letterdef pick_option():    user_pick = input("Choose Rock, Paper, Scissors, Lizard, Spock: ")    if user_pick in ["Rock", "rock", "r", "R"]:        user_pick = "r"    elif user_pick in ["Paper", "paper", "p", "P"]:        user_pick = "p"    elif user_pick in ["Scissors", "scissors", "sc", "SC"]:        user_pick = "sc"    elif user_pick in ["Lizard", "lizard", "l", "L"]:        user_pick = "l"    elif user_pick in ["Spock", "spock", "sp", "SP"]:        user_pick = "sp"    else:        print("Are you speaking english buddy?")        pick_option()    return user_pick# this will act as our function for the computer to choose# they will random select a number 1-5# these number are assinged to one of the five options the user hasdef comp_option():    comp_pick = randint(1,5)    if comp_pick == 1:        comp_pick = "r"    if comp_pick == 2:        comp_pick ="p"    if comp_pick == 3:        comp_pick = "sc"    if comp_pick == 4:        comp_pick = "l"    if comp_pick == 5:        comp_pick = "sp"    return comp_pick所以我遇到的麻煩就是這樣。游戲運行良好,一旦計算機或用戶取得 3 場勝利,我們就會收到獲勝或失敗的消息。發(fā)生這種情況后,系統(tǒng)會要求您再次玩游戲。如果您點擊“否”,則代碼結束。如果您點擊“是”,游戲將正確重新啟動,但在您第一次選擇時,它會將您帶回獲勝或失敗屏幕。似乎勝利計數器沒有重置或者我有不正確的縮進?任何幫助表示贊賞。PS:我知道可能還有一種更簡單的方法來制作游戲,而不是使用所有這些 elif 語句。也許是上課什么的?雖然我是新手,所以這對我來說是最簡單的。
查看完整描述

3 回答

?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

一些旁注建議:

  • 要檢查變量是否是一系列字符串之一,您可以通過執(zhí)行以下操作使其更加緊湊:

    if user_pick.lower() in ["rock", "r"]:
  • 無需將 int 轉換為 int :) int(1) == 1

  • endgame充當旗幟 - 游戲是否結束。True因此,使用布爾值 - / False- 比使用 int更有意義。


現(xiàn)在您的主要問題是您的代碼endgame根據player_wins和設置變量comp_wins。當您開始新游戲時,這些值會保留舊值,因此一輪游戲結束后立即再次結束。只需重置代碼最后部分中的所有關鍵變量:

endgame = False

...

if player_wins >= 3: 

    endgame = True

...

if comp_wins >= 3:

    endgame = True

...

if endgame:

    user_pick = input("Do you want to play again (y/n)?")

    if user_pick.lower() in ["y", "yes"]:

        endgame = False

        player_wins = 0

        comp_wins = 0

    else:

        break

附注建議:

通常重復的if//樹結構elif可以else被減少。在其他語言中,您有這樣的switch語句 - 這有點讓代碼看起來相同,只是與if/else. 在 Python 中,您通常可以將大多數if樹轉換為字典中的選擇。所以你的comp_option可以改為:


def comp_option():

    comp_pick = randint(1,5)

    options = {1: 'r', 2: 'p', 3: 'sc', 4: 'l', 5: 'sp'}

    return options[comp_pick]


查看完整回答
反對 回復 2023-12-20
?
慕容708150

TA貢獻1831條經驗 獲得超4個贊

我認為您必須添加一些額外的語句,以便在用戶選擇重新啟動游戲時重置游戲內的統(tǒng)計數據。就像是:


 if int(endgame) == int(1):

        user_pick = input("Do you want to play again (y/n)?")

        if user_pick in ["Y", "y", "yes", "Yes"]:

            comp_wins = 0

            player_wins = 0

            endgame = 0

            continue


查看完整回答
反對 回復 2023-12-20
?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

你發(fā)現(xiàn)問題了!您必須重置殘局計數。為此,只需將endgame變量放入循環(huán)中while True(在開頭)

while True:

   endgame = 0

這應該有效。


查看完整回答
反對 回復 2023-12-20
  • 3 回答
  • 0 關注
  • 200 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

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

公眾號

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