In [ ]:
# Imports
from IPython.display import clear_output
from random import randint
from time import sleep
def endFunct(): # Exit routine
clear_output()
print("{} wins : {}" .format(userName, userWin))
print("Computer wins : {}" .format(comWin))
print("Would you like to try again? Y/N")
choice = input("Enter Y or N.")
if choice == "Y":
clear_output()
gameFunct()
elif choice == "N":
print("See you later.")
def gameFunct(): # Main game function
opt = ["Rock", "Paper", "Scissors"]
global userWin
global comWin
print("Hello. I am the HAL 9000 supercomputer. I would like to play a game of Rock, Paper, Scissors with you.\nPlease, pick a number.\n1. Rock\n2. Paper\n3. Scissors")
try:
userChoice = int(input("Pick a number between 1 and 3.")) - 1
userCh = opt[userChoice] # Picks option from list based on number user picked
comCh = opt[comChoice] # Picks option from list besed on random integer generated
print("{} chose {}." .format(userName, userCh)) # Displays user choice
print("The computer chose {}." .format(comCh)) # Displays computer's choice
if userCh == comCh: # Tie condition
print("Tied.")
sleep(2)
endFunct()
elif userCh == "Rock": # Win condition
if comCh == "Scissors":
print("You win!")
sleep(2)
userWin += 1
endFunct()
else:
print("Sorry, you lose.")
sleep(2)
comWin += 1
endFunct()
elif userCh == "Scissors": # Win condition
if comCh == "Paper":
print("You win!")
sleep(2)
userWin += 1
endFunct()
else:
print("Sorry, you lose.")
sleep(2)
comWin += 1
endFunct()
elif userCh == "Paper": # Win condition
if comCh == "Rock":
print("You win!")
sleep(2)
userWin += 1
endFunct()
else:
print("Sorry, you lose.")
sleep(2)
comWin += 1
endFunct()
except IndexError: # Prevents program crash if a number that isn't 1 through 3 is not entered
print("I requested a number between 1 and 3.")
sleep(2)
endFunct()
except ValueError: # Prevents program crash if a number is not entered
print("I'm sorry, but you did not input a number.")
sleep(2)
endFunct()
if __name__ == "__main__": # Variable declaration & name input field
userName = input("Enter your name.")
comChoice = randint(0,2)
userWin = 0
comWin = 0
gameFunct()