我正在尝试用累加器编写一个用于摇滚,纸张和剪刀的python程序,我无法弄清楚如何使累加器工作。这是我的代码。任何有关该问题的帮助将不胜感激。
import random
def main():
player_wins = 0
computer_wins = 0
ties = 0
total_games = 0
print("Welcome to rock, paper, and scissors.")
again = "y"
while again == "y":
play_games(ties,computer_wins, player_wins, total_games)
again = input("Would you like to play?")
if again != "y":
accumulator(ties,computer_wins, player_wins,total_games)
def get_computers_choice():
for count in range(1):
computers_choice = random.randint(1,3)
if computers_choice == 1:
computers_choice = "rock"
elif computers_choice == 2:
computers_choice = "paper"
else:
computers_choice = "scissors"
return computers_choice
def get_players_choice():
players_choice = input("Enter 1 for rock, 2 for paper, or 3 for scissors: ")
if players_choice == "1":
players_choice = "rock"
elif players_choice == "2":
players_choice = "paper"
elif players_choice == "3":
players_choice = "scissors"
else:
print("Error choice must be 1 for rock, 2 for paper, or 3 for scissors")
get_players_choice()
return players_choice
def play_games(ties,computer_wins, player_wins,total_games):
computers_choice = get_computers_choice()
players_choice = get_players_choice()
if computers_choice == "rock":
print("The computer's choice is rock.")
elif computers_choice == "paper":
print("The computer's choice is paper.")
else:
print("The computer's choice is scissors.")
winner = determine_the_winner(computers_choice, players_choice,computer_wins, player_wins, total_games, ties)
return winner,ties,computer_wins, player_wins,total_games
def determine_the_winner(computers_choice, players_choice,ties,computer_wins,
player_wins,total_games):
if computers_choice == players_choice:
print("Game is a tie.")
ties = ties + 1
total_games = total_games + 1
return total_games, ties
elif computers_choice == "rock" and players_choice == "scissors":
print("You lose, rock crushes scissors.")
computer_wins = computer_wins + 1
total_games = total_games + 1
return computer_wins, total_games
elif computers_choice == "rock" and players_choice == "paper":
print("You win, paper covers rock.")
player_wins = player_wins + 1
total_games = total_games + 1
return player_wins, total_games
elif computers_choice == "paper" and players_choice == "rock":
print("You lose, paper covers rock.")
computer_wins = computer_wins + 1
total_games = total_games + 1
return computer_wins, total_games
elif computers_choice == "paper" and players_choice == "scissors":
print("You win, scissors cuts paper.")
player_wins = player_wins + 1
total_games = total_games + 1
return player_wins, total_games
elif computers_choice == "scissors" and players_choice == "paper":
print("You lose, scissors cuts paper.")
computer_wins = computer_wins + 1
total_games = total_games + 1
return computer_wins, total_games
elif computers_choice == "scissors" and players_choice == "rock":
print("You win, rock crushes scissors.")
player_wins = player_wins + 1
total_games = total_games + 1
return player_wins, total_games
def accumulator(ties,computer_wins, player_wins,total_games):
print("You have played", total_games, "games")
print("You won", player_wins, "games")
print("You lost", computer_wins, "games")
print("You tied", ties, "games")
main()
答案 0 :(得分:1)
您应该创建一个对象来保存您的分数,并determine_the_winner
更新其字段:
import random
class Score:
def __init__(self):
self.total_games = 0
self.ties = 0
self.player_wins = 0
self.computer_wins = 0
def main():
score = Score()
print "Welcome to rock, paper, and scissors."
again = "y"
while again == "y":
play_game(score)
again = raw_input("Would you like to play again (y/n)? ")
accumulator(score)
def get_computers_choice():
return random.choice(["rock", "paper", "scissors"])
def get_players_choice():
choices = {"1":"rock", "2":"paper", "3":"scissors"}
while True:
players_choice = raw_input("Enter 1 for rock, 2 for paper, or 3 for scissors: ")
if players_choice in choices:
return choices[players_choice]
print "Error choice must be 1 for rock, 2 for paper, or 3 for scissors"
def play_game(score):
computers_choice = get_computers_choice()
players_choice = get_players_choice()
print "The player's choice is {choice}.".format(choice=players_choice)
print "The computer's choice is {choice}.".format(choice=computers_choice)
determine_the_winner(computers_choice, players_choice, score)
def determine_the_winner(computers_choice, players_choice, score):
rules = [
('rock', 'crushes', 'scissors'),
('scissors', 'cuts', 'paper'),
('paper', 'covers', 'rock')
]
score.total_games += 1
for good, verbs, bad in rules:
if computers_choice == good and players_choice == bad:
outcome = "lose"
score.computer_wins += 1
elif players_choice == good and computers_choice == bad:
outcome = "win"
score.player_wins += 1
else:
continue
print "You {outcome}, {good} {verbs} {bad}.".format(outcome=outcome,
good=good,
verbs=verbs,
bad=bad)
break
else:
score.ties += 1
print("Game is a tie.")
def accumulator(score):
print "You have played", score.total_games, "games"
print "You won", score.player_wins, "games"
print "You lost", score.computer_wins, "games"
print "You tied", score.ties, "games"
main()