This is a follow-up to my previously posted "Rock, Paper, Scissors" game. I did almost everything I was recommended in the answers and I hope you like it.
import os
from random import choice
def player_choice():
while True:
print("Rock, paper or scissors?")
choice = input(">").capitalize()
if choice in ('Rock', 'Paper', 'Scissors'):
return choice
def win_msg(player_choice, computer_choice):
msg = f"{player_choice} beats {computer_choice}. You won!"
return msg
def lose_msg(player_choice, computer_choice):
msg = f"{computer_choice} beats {player_choice}. You lost!"
return msg
def computer_choice():
return choice(('Rock', 'Paper', 'Scissors'))
def show_statistics(scores):
os.system('cls' if os.name == 'nt' else 'clear')
print(f"Wins: {scores[0]}\nLosses: {scores[1]}\nDraws: {scores[2]}")
def game_outcome(player, computer):
if player == computer:
return 'Draw'
elif player == 'Rock':
if computer == 'Paper':
return 'Lose'
else:
return 'Win'
elif player == 'Paper':
if computer == 'Rock':
return 'Win'
else:
return 'Lose'
else:
if computer == 'Rock':
return 'Lose'
else:
return 'Win'
def show_results(outcome, player, computer):
if outcome == 'Win':
print(win_msg(player, computer))
elif outcome == 'Lose':
print(lose_msg(player, computer))
else:
print("Draw. Nobody wins or losses.")
def update_scores(scores, outcome):
new_scores = list(scores)
if outcome == 'Win':
new_scores[0] += 1
elif outcome == 'Lose':
new_scores[1] += 1
else:
new_scores[2] += 1
new_scores = tuple(new_scores)
return new_scores
def rock_paper_scissors(scores):
player = player_choice()
computer = computer_choice()
outcome = game_outcome(player, computer)
show_results(outcome, player, computer)
new_scores = update_scores(scores, outcome)
return new_scores
def play_again():
while True:
print("\nDo you want to play again?")
print("(Y)es")
print("(N)o")
ans = input("> ").lower()
if ans == 'y':
return True
elif ans == 'n':
return False
def starting_scores():
return 0, 0, 0
def main():
scores = starting_scores()
still_playing = True
while still_playing:
show_statistics(scores)
scores = rock_paper_scissors(scores)
still_playing = play_again()
if __name__ == '__main__':
main()