Python:在游戏中添加计数器

问题描述 投票:0回答:6

我对 python 和一般编程都很陌生,我发现循环非常令人困惑。我并不经常需要使用循环,但我目前正在开发一个简单的游戏(Foot,Nuke,Cockroach,类似于Rock,Pa,Scissors),我很确定我在基本逻辑上取得了成功,但是我无法获得如何为回合制作计数器的逻辑,因为我必须将其与 if 语句(我猜)或类似的东西联系起来。我还希望能够计算玩家获胜的回合数以及平局的次数。玩家正在与计算机对战,计算机根据随机数生成回复。

import random

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):")

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):")

有人可以建议我如何处理这个循环吗? 编辑:我希望在用户退出后打印胜利、平局和回合的数量。

python loops if-statement counter
6个回答
0
投票

嗯,您可以在每个 if 中添加一个 return 语句。如果平局则返回 0,如果玩家获胜则返回 1,否则返回 2。

然后只需添加

result = choice(chosen1, chosen2)

结果如下。然后将其与更多的 if 进行比较,并添加平局和获胜变量。 虽然很难看,但应该可以用。


0
投票

你的循环看起来没问题。您只需将结果保存在可以更新的变量中。您可以在名为

choice
的函数中为每个结果使用 return 语句,或者只需要一点点努力即可定义

wins = 0
ties = 0

在程序的开头,然后添加一个

wins += 1
# or ties += 1

你的计划的每一个可能的结果


0
投票

基本上:你需要声明一个计数变量,并在每次新一轮开始时将变量加1,然后,你可以打印轮数。如果你使用了数字(就像你应该的那样),你会得到一个错误,要解决这个问题,请使用 str() 方法。

我已经在 repl.it 上实现了这个 https://repl.it/@conradkay/WarpedPrevailingQuerylanguage


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

然后查看状态

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()

0
投票
import random

options = ['Foot','Nuke','Cockroach']
computer_number = random.randint(0,2)
computer_choice = options[computer_number]
def choice(chosen1, chosen2):
    print("You chose: ", chosen2)
    print("Computer chose: ", chosen1)
    if (computer_choice,player_choice) == ('Nuke','Nuke'):
        print("You LOSE!")
        return 'Loss'
    if (computer_choice==player_choice):
        print("It is a tie!")
        return 'Tie'    
    player_number = options.index(player_choice)
    result = (player_number-computer_number)%3
    if result == 2:        
        print(['You LOSE'])
        return 'Loss'
    if result == 1:
        print(['You WIN!'])
        return 'Win'

result_counts = {'Win':0,'Loss':0,'Tie':0}

while True:
    player_choice = input("Foot, Nuke or Cockroach? (Quit ends):")
    if player_choice == 'Quit':
        break
    result = choice(computer_choice, player_choice)
    results_counts[result] = results_counts[result]+1

请注意,如果您希望计算机每次都做出不同的选择,则应放置以下行

computer_number = random.randint(0,2)
computer_choice = options[computer_number]

while
循环内。另请注意,您忽略了很多有效性检查。


0
投票
import random

playedRound = 0
wonRound = 0
tieRound = 0

while True:
userInput = input("Foot, Nuke or Cockroach? (Quit ends): ").capitalize()
number = random.randint(1, 3)
computerChoice = ""

if number == 1:
    computerChoice = "Foot"
elif number == 2:
    computerChoice = "Nuke"
elif number == 3:
    computerChoice = "Cockroach"



if userInput not in ["Foot", "Nuke", "Cockroach", "Quit"]:
    print("Incorrect selection.")
else:
    if userInput == "Quit":
        print("You played", playedRound, "rounds,and won", wonRound, "rounds, playing tie in", tieRound, "rounds.")
        break

    if userInput == computerChoice:
        print("You chose:", userInput)
        print("Computer chose:", computerChoice)
        print("It is a tie!")
        tieRound += 1
        playedRound += 1
    elif (userInput == "Foot" and computerChoice == "Cockroach") or \
         (userInput == "Nuke" and computerChoice == "Foot") or \
         (userInput == "Cockroach" and computerChoice == "Nuke"):
        print("You chose:", userInput)
        print("Computer chose:", computerChoice)
        print("You WIN!")
        wonRound += 1
        playedRound += 1
    else:
        print("You chose:", userInput)
        print("Computer chose:", computerChoice)
        print("You LOSE!")
        playedRound += 1
© www.soinside.com 2019 - 2024. All rights reserved.