在使用多个if语句时如何存储数字?

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

我正在创建用于岩石,纸张,剪刀的python代码,但我似乎无法跟踪获胜,失败和平局的得分。我认为我的“计数”出了点问题,我称之为“赢+ = 0”,“输+ = 0”和“领带+ = 0”。有人可以告诉我,每当有赢,输或平局时,我应该怎么做才能将分数提高1?

下面是我的代码:

from random import randint

t = ["rock", "paper", "scissors"]
computer = t[randint(0,2)]
player = False
lose = 0
win = 0

for i in range(0,10):
    print("1... 2... 3... go!")
    while player == False:
        player = input("rock, paper, scissors: ")
        print("Computer: ", computer)
        print("User: ", player)
        if player == computer:
            tie = 0
            tie +=1
            print("Tie!")
        elif player == "rock":
            if computer == "paper":
                lose +=0
                print("You lose!")
            else:
                win +=0
                print("You win!")
        elif player == "paper":
            if computer == "scissors":
                lose = 0
                lose +=0
                print("You lose!")
            else:
                win +=0
                print("You win!")
        elif player == "scissors":
            if computer == "rock":
                lose +=0
                print("You lose!")
            else:
                win +=0
                print("You win!")
        else:
            print("That's not a valid play. Check your spelling!")

        player = False
        computer = t[randint(0,2)]
        break
print("Final Tally")
print("************")
print("User Wins: ", win)
print("Computer Wins: ", lose)
print("Ties: ", tie)

if tie > win and tie > lose:
    print("It's a tie!")
elif win > tie and win > lose:
    print("You won!")
else:
    print("The computer won!")
python if-statement count
1个回答
1
投票

这里是固定版本。我建议您再做一些工作:)

from random import choice

t = ["rock", "paper", "scissors"]

tie = 0
lose = 0
win = 0

for i in range(0, 10):
    print("1... 2... 3... go!")

    # you need to refresh these variables on every for iteration
    computer = choice(t)
    player = None

    # if you're using while to make sure player inputs, that's the only thing that needs
    # to be within the while loop
    while not player:
        player = input("rock, paper, scissors: ")
    print("Computer: ", computer)
    print("User: ", player)

    # I would look for a way to simplify determining the winner
    if player == computer:
        # tie += 1 is the same as tie = tie + 1
        tie +=1
        print("Tie!")
    elif player == "rock":
        if computer == "paper":
            lose += 1
            print("You lose!")
        else:
            win += 1
            print("You win!")
    elif player == "paper":
        if computer == "scissors":
            lose += 1
            print("You lose!")
        else:
            win += 1
            print("You win!")
    elif player == "scissors":
        if computer == "rock":
            lose += 1
            print("You lose!")
        else:
            win += 1
            print("You win!")
    else:
        print("That's not a valid play. Check your spelling!")


print("Final Tally")
print("************")
print("User Wins: ", win)
print("Computer Wins: ", lose)
print("Ties: ", tie)

if tie > win and tie > lose:
    print("It's a tie!")
elif win > tie and win > lose:
    print("You won!")
else:
    print("The computer won!")

更新:显然我无事可做。好的,这是简化获胜条件的直接方法。

    win_condition_rock = player == 'rock' and computer == 'scissors'
    win_condition_paper = player == 'paper' and computer == 'rock'
    win_condition_scissors = player == 'scissors' and computer == 'paper'

    if player == computer:
        # tie += 1 is the same as tie = tie + 1
        tie +=1
        print("Tie!")

    elif any([win_condition_paper, win_condition_scissors, win_condition_rock]):
        win += 1
        print('You win')

    else:
        lose += 1
        print('You lose')

更新2:这是对有效输入的检查

    while player not in t:
        player = input("rock, paper, scissors: ").lower()
        if player not in t:
            print('Invalid input')
© www.soinside.com 2019 - 2024. All rights reserved.