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

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

Python:在游戲上添加計數器

Python:在游戲上添加計數器

手掌心 2021-05-05 13:12:58
我是python和一般編程人員的新手,我發現循環非?;靵y。我不需要經常使用循環,但是我目前正在開發一個簡單的游戲(腳,Nuke,蟑螂,類似于Rock,Paper和Scissors),而且我很確定我的基本邏輯成功了因為我必須將它與if語句(我猜)或類似的東西綁定在一起,所以無法獲得關于如何在回合中進行計數的邏輯。我還希望能夠計算出玩家贏得了多少回合以及多少次是平局。玩家正在與基于隨機數生成其答復的計算機對戰。import randomnumber = random.randint(1,3)if number == 1:    chosen1 = "Foot"elif number == 2:    chosen1 = "Nuke"else:    chosen1 = "Cockroach"chosen2 = input("Foot, Nuke or Cockroach? (Quit ends):")def choice(chosen1, chosen2):    if (chosen1 == "Nuke" and chosen2=="Nuke"):        print("You chose: ", chosen2)        print("Computer chose: ", chosen1)        print("You LOSE!")    elif chosen1 == chosen2:        print("You chose: ", chosen2)        print("Computer chose: ", chosen1)        print("It is a tie!")    elif (chosen1 == "Foot" and chosen2=="Cockroach"):        print("You chose: ", chosen2)        print("Computer chose: ", chosen1)        print("You LOSE!")    elif (chosen2 == "Foot" and chosen1=="Cockroach"):        print("You chose: ", chosen2)        print("Computer chose: ", chosen1)        print("You WIN!")    elif (chosen1 == "Nuke" and chosen2=="Foot"):        print("You chose: ", chosen2)        print("Computer chose: ", chosen1)        print("You LOSE!")    elif (chosen2 == "Nuke" and chosen1=="Foot"):        print("You chose: ", chosen2)        print("Computer chose: ", chosen1)        print("You WIN!")    elif (chosen1 == "Cockroach" and chosen2=="Nuke"):        print("You chose: ", chosen2)        print("Computer chose: ", chosen1)        print("You LOSE!")    elif (chosen2 == "Cockroach" and chosen1=="Nuke"):        print("You chose: ", chosen2)        print("Computer chose: ", chosen1)        print("You WIN!")    elif chosen2 =="Quit":        quit()while chosen2 != "Quit":    choice(chosen1, chosen2)    chosen2 = input("Foot, Nuke or Cockroach? (Quit ends):")有人可以建議我如何處理此循環嗎?編輯:我希望在用戶退出后打印獲勝,平局和回合的數量。
查看完整描述

3 回答

?
浮云間

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

您可以像這樣對贏和輸進行查表:


who_wins = {('Cockroach','Nuke'): True,

            ('Foot', 'Cockroach'): True,

            ('Nuke', 'Foot'): True}

這樣,您可以運行像這樣的函數


def check_winner(player, cpu):

    result = who_wins.get((player,cpu), False)

    return result

然后檢查狀態


result = check_winner(chosen2, chosen1)

if result:

   print("You won!")

   your_wins+=1

else:

   print("You lost!")

   computer_wins+=1

總共看起來像


import random


your_wins, cpu_wins = 0,0


who_wins = {('Cockroach','Nuke'): True,

            ('Foot', 'Cockroach'): True,

            ('Nuke', 'Foot'): True}


def check_winner(player, cpu):

    result = who_wins.get((player,cpu), False)

    return result


while True:

    number = random.randint(1,3)


    if number == 1:

        chosen1 = "Foot"

    elif number == 2:

        chosen1 = "Nuke"

    else:

        chosen1 = "Cockroach"


    chosen2 = input("Foot, Nuke or Cockroach? (Quit ends):")


    if chosen2.lower() != "quit":

        result = check_winner(chosen2, chosen1)

        if result:

           print("You won!")

           your_wins += 1

        else:

           print("You lost!")

           cpu_wins +=1

    else:

        quit()


查看完整回答
反對 回復 2021-05-25
  • 3 回答
  • 0 關注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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