Skip to content

Instantly share code, notes, and snippets.

@jonmoshier
Created August 3, 2018 18:16
Show Gist options
  • Save jonmoshier/0244bb87dfe202b9d735f09ffcd7d512 to your computer and use it in GitHub Desktop.
Save jonmoshier/0244bb87dfe202b9d735f09ffcd7d512 to your computer and use it in GitHub Desktop.
# Rock, Paper, Scissors command line python3 game
# run at commandline by typing `python rock-paper-scissors.py`
from random import randint # import random for computer choice
class NotAChoiceException(Exception): # creating a custom exception to handle by inputs
"""doc statement about error"""
pass
def takeInput(): # the function that handles input
nb = input('Enter 0, 1 or 2: ') # waits for user input
try: # try is for places where you check for errors
choice = int(nb) # converts input string to integer
if choice > 2: # if number too high, throw custom exception
raise NotAChoiceException
return choice # else return our choice to where function was called
except (ValueError, NotAChoiceException): # catches if user input is not a number or number greater than 2
print("I need a number 0 - Rock, 1 - Paper or 2 - Scissors")
return takeInput() # calls itself to start over
def playAgain():
ans = str(input('Play again (Y/N): ')) # take input
try:
if ans.lower() in ("y", "yes"): # if lowercased answer is in y, yes
main() # call main function again, start over
if ans.lower() in ("n", "no"): # lowercased answer is n or no
print("Thanks, bye!") # be courteous
exit() # and exit
else:
raise NotAChoiceException
except NotAChoiceException:
print("Y/N please")
playAgain()
# using """ for multiline string
print("""Hello, this is Rock, Paper, Scissors.
The rules are easy. You pick Rock, Paper or Scissors and the computer will pick one as well.
Rock beats Scissors, Scissors beats Paper and Paper beat Rock
Type 0 for Rock, 1 for Paper or 2 for Scissors.""")
computer_score = 0 # initialize score
user_score = 0 # initialize score
def main():
computer_choice = randint(0,2) # computer choice is random int 0-2
user_choice = takeInput() # calling takeInput() to handle users choice
global user_score, computer_score # using global scope scores
# Rules here..
rps = ["Rock", "Paper", "Scissors"] # array for names indexed to 0,1,2
if user_choice == computer_choice: # case where there's a draw
print("Draw - User and Computer both picked {0}".format(rps[user_choice]))
elif user_choice == 0 and computer_choice == 2: # else if handle rock beats scissors
print("Human wins - Rock beats computer's Scissors")
user_score += 1 # increment score for human
elif computer_choice == 0 and user_choice == 2: # handle case where rock beats scissors for computer
print("Computer wins - Rock beats human's Scissors")
computer_score += 1
elif user_choice > computer_choice: # otherwise 2 beats 1 and 1 beats 0, greater number wins.
print("Human wins with {} beating computers {}".format(rps[user_choice], rps[computer_choice]))
user_score += 1
elif user_choice < computer_choice:
print("Computer wins with {} beating humans {}".format(rps[computer_choice], rps[user_choice]))
computer_score += 1
print("H:{}, C:{}".format(user_score, computer_score))
playAgain() # calls function to ask to play again
if __name__ == "__main__":
main() # the programs entry point, calls the main() function
@st0626
Copy link

st0626 commented Aug 3, 2018

you made yours way more in depth than mine lol i can learn a lot from this, much appreciated

@bradleygrant
Copy link

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment