Last active
July 2, 2016 19:57
-
-
Save cornfeedhobo/07aea1922c61cd9fa8c7e12703982e73 to your computer and use it in GitHub Desktop.
Rock Paper Scissor - intro to 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
"""The great game of Rock Paper Scissor""" | |
import os | |
import random | |
def play(rounds): | |
"""Play game() N times and present a scoreboard when complete""" | |
user_score = 0 | |
comp_score = 0 | |
for _ in range(0, rounds): | |
if game() is True: | |
user_score += 1 | |
else: | |
comp_score += 1 | |
# clear the screen | |
os.system('cls' if os.name == 'nt' else 'clear') | |
print('-----------') | |
print('Scoreboard:') | |
print('-----------') | |
print('User: {}'.format(user_score)) | |
print('Computer: {}'.format(comp_score)) | |
print('') | |
print('-----------') | |
if user_score > comp_score: | |
print('YOU WON!') | |
else: | |
print('YOU LOST!') | |
def game(): | |
"""Single RPS round, returning user outcome boolean""" | |
os.system('cls' if os.name == 'nt' else 'clear') | |
print("Let's Play!") | |
print('-----------') | |
print('') | |
print('1) Rock') | |
print('2) Paper') | |
print('3) Scissor') | |
print('') | |
user_selection = input('Make a selection (numbers and words accepted): ') | |
valid_selection = { | |
1: 'rock', | |
2: 'paper', | |
3: 'scissor', | |
} | |
try: | |
key = int(user_selection) | |
if key in valid_selection.keys(): | |
user_selection = valid_selection[key] | |
else: | |
return game() | |
except ValueError: | |
if user_selection.lower() in valid_selection.values(): | |
user_selection = user_selection.lower() | |
else: | |
return game() | |
# get the computer's selection | |
computer_selection = random.randint(1, 3) | |
computer_selection = valid_selection[computer_selection] | |
# Handle possible outcomes | |
if user_selection == computer_selection: | |
input('\nYOU TIED! TRY AGAIN ...') | |
return game() | |
elif (user_selection == 'rock' and computer_selection == 'paper') \ | |
or (user_selection == 'paper' and computer_selection == 'scissor') \ | |
or (user_selection == 'scissor' and computer_selection == 'rock'): | |
input('\nYOU LOSE') | |
return False | |
else: | |
input('\nYOU WIN') | |
return True | |
def rock_paper_scissor(): | |
"""Main entry point for the game""" | |
os.system('cls' if os.name == 'nt' else 'clear') | |
# present the menu | |
print('Rock - Paper - Scissor') | |
print('----------------------') | |
print('') | |
print('1) Quick Draw') | |
print('2) Best out of 3') | |
print('3) Exit') | |
print('') | |
selection = input('Make a selection: ') | |
# Check for a integer input | |
try: | |
selection = int(selection) | |
except ValueError: | |
return rock_paper_scissor() | |
# Check that it's a valid option | |
valid_selection = [1, 2, 3] | |
if selection not in valid_selection: | |
return rock_paper_scissor() | |
# at this point, selection is valid. operate on it | |
if selection == 1: | |
play(1) | |
elif selection == 2: | |
play(3) | |
elif selection == 3: | |
exit() | |
if __name__ == '__main__': | |
rock_paper_scissor() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment