I am a newbie to Python and am trying to do the Rock, Paper and Scissors game to revise my knowledge that I have earned so far.
Overview of the program:
- This is a two-player game
- Ask for player plays (including name, their choice: rock or paper or scissors using input)
- Print out a message of winner
- Ask if the user want to start a new game
This code is written in procedural style.
Can anyone show me some weaknesses in my code and how I can improve on them?
def beats(a, b):
tup = ('rock', 'paper', 'scissors')
if (a == tup[1] and b == tup[2]) or (a == tup[2] and b == tup[1]):
return tup[2]
elif (a == tup[1] and b == tup[0]) or (a == tup[0] and b == tup[1]):
return tup[1]
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?: ')
if decision == 'rock' or decision == 'paper' or decision == 'scissors':
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!")
play()
play_again()