-
-
Save sometowngeek/cb18d338bd488c45fccefc86bb9a9b1d to your computer and use it in GitHub Desktop.
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
def beats(a, b): | |
tup = ('rock', 'paper', 'scissors') | |
# if a == 'paper' and b == 'scissors' or a == 'scissors' and b == 'paper' | |
# return 'scissors' | |
if (a == tup[1] and b == tup[2]) or (a == tup[2] and b == tup[1]): | |
return tup[2] | |
# elif a == 'paper' and b == 'rock' or a == 'rock' and b == 'paper' | |
# return 'paper' | |
elif (a == tup[1] and b == tup[0]) or (a == tup[0] and b == tup[1]): | |
return tup[1] | |
# elif a == 'rock' and b == 'scissors' or a == 'scissors' and b == 'rock' | |
# return 'rock' | |
elif (a == tup[0] and b == tup[2]) or (a == tup[2] and b == tup[0]): | |
return tup[0] | |
def players_input(): | |
name = input('What is your name?: ') | |
while True: | |
decision = input('Paper, Rock, or Scissors?: ') | |
# This will check whether the decision exists in a given tuple. | |
# This helps simplify "if x == 5 or x == 3 or x == 1" by putting it into | |
# if x in (1, 3, 5) | |
if decision.lower() in ('rock', 'paper', 'scissor'): | |
break | |
else: | |
print("Invalid choice! Try again!") | |
continue | |
return '{} chooses {}'.format(name, decision) | |
def play(): | |
player1 = players_input() | |
player2 = players_input() | |
print('{}\n{}'.format(player1, player2)) | |
name1, verb, decision1 = player1.split(" ") | |
name2, verb, decision2 = player2.split(" ") | |
returnvalue = beats(decision1.lower(), decision2.lower()) | |
if decision1.lower() == returnvalue and decision2.lower() != returnvalue: | |
print('{} wins'.format(name1)) | |
elif decision2.lower() == returnvalue and decision1.lower() != returnvalue: | |
print('{} wins'.format(name2)) | |
else: | |
print('It\'s a tie') | |
def play_again(): | |
while True: | |
choice = input('Do you want to continue?(Y/N): ') | |
if choice.upper() == "Y": | |
play() | |
elif choice.upper() == "N": | |
print("Game over") | |
break | |
else: | |
print("Invalid Input! Try again!") | |
# You should always have this if you're doing unit testing. | |
if __name__ == '__main__': | |
play() | |
play_again() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment