Skip to content

Instantly share code, notes, and snippets.

@erdavids
Created November 25, 2019 19:11
Show Gist options
  • Save erdavids/fb694c980c1146bd386dc642004f3077 to your computer and use it in GitHub Desktop.
Save erdavids/fb694c980c1146bd386dc642004f3077 to your computer and use it in GitHub Desktop.
import random
# First element is the attack, the following elements are weaknesses
attacks = [
['rock', 'paper'],
['paper', 'scissors'],
['scissors', 'rock']
]
# Tracking game progress
games_won = 0
games_lost = 0
def play_game():
global games_won
global games_lost
# Ask for the player's attack
user_input = input('Your move: ')
print()
# Make sure the user input is valid
valid_input = False
for att in attacks:
if user_input == att[0]:
valid_input = True
user_attack = att
if (valid_input != True):
print('You can\'t use that attack!')
exit()
# Choose a random attack for the computer
computer_attack = random.choice(attacks)
print(f'You used: {user_attack[0]}')
print(f'The Computer used: {computer_attack[0]}')
print()
# See who wins by comparing attack weaknesses
if (user_attack[0] == computer_attack[0]):
print('It\'s a tie!')
elif (computer_attack[0] in user_attack):
print('You lost this round!')
games_lost += 1
elif (user_attack[0] in computer_attack):
print('You won this round!')
games_won += 1
if __name__ == "__main__":
games_to_play = int(input('How many games would you like to play? '))
games_to_win = int(games_to_play/2 + 1)
print()
print(f'You need to win {games_to_win} games')
# Play until someone wins enough rounds
while(games_won < games_to_win and games_lost < games_to_win):
play_game()
print(f'You: {games_won}')
print(f'Computer: {games_lost}')
print()
print(f'Final Score: {games_won} - {games_lost}')
if (games_won > games_lost):
print('You beat the Computer!')
else:
print('Better luck next time!')
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment