-
-
Save zunayed/f709986dd969b7594e19501f292efd92 to your computer and use it in GitHub Desktop.
Rock Paper Scissors game (Python)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import random | |
GAME_CHOICES = ['rock', 'paper', 'scissors'] | |
def random_choice(): | |
return random.choice(GAME_CHOICES) | |
def print_game_info(): | |
time.sleep(1) | |
print('rock...') | |
time.sleep(1) | |
print('paper...') | |
time.sleep(1) | |
print('scissors...') | |
print('SHOOT!') | |
time.sleep(1) | |
def choice_result(human_choice, computer_choice): | |
result_map = { | |
('paper', 'rock'): True, | |
('paper', 'scissors'): False, | |
('rock', 'paper'): False, | |
('rock', 'scissors'): True, | |
('scissors', 'paper'): True, | |
('scissors', 'rock'): False | |
} | |
if human_choice == computer_choice: | |
print('You chose {human_choice} and the computer chose {computer_choice}. It is a tie') | |
return 0 | |
game_result = result_map[(human_choice, computer_choice)] | |
if game_result: | |
print('You chose {human_choice} and the computer chose {computer_choice}. You win!') | |
return 1 | |
else: | |
print('You chose {human_choice} and the computer chose {computer_choice}. You lose, good day sir!') | |
return 2 | |
def play(): | |
keep_playing = True | |
human_score = 0 | |
computer_score = 0 | |
while keep_playing: | |
choice = input("Make your choice: rock, paper, or scissors? ") | |
if choice not in GAME_CHOICES: | |
print('That is not an available option. Please try again.') | |
continue | |
computer_choice = random_choice() | |
print_game_info() | |
game_result = choice_result(choice, computer_choice) | |
if game_result == 1: | |
human_score += 1 | |
elif game_result == 2: | |
computer_score += 1 | |
print('\n') | |
print("Your wins: " + str(human_score)) | |
print("Computers wins: " + str(computer_score)) | |
print('\n') | |
if input("Do you want to play again? ")[0].lower() != 'y': | |
keep_playing = False | |
play() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment