patternpythonMinor
Rock, Paper, Scissors game assignment
Viewed 0 times
scissorspaperassignmentgamerock
Problem
This is an assignment that I have already turned in. It works great, but I was wondering if there are any flaws in the code, or how to write it in a more pythonic way.
```
import random
tie = 0
pcWon = 0
playerWon = 0
# Displays program information, starts main play loop, after main loop is executed (user pressing 4 in menu), display
# the scores to the user.
def main():
print("A game or rock, paper, or scissors!")
playGame = True
while playGame:
playGame = play()
displayScoreBoard()
prompt = input("press enter to exit")
# displays the menu for user, if input ==4, playGame in the calling function (main()) is False, terminating the program.
# Generate a random int 1-3, evaluate the user input with the computer input, update globals accordingly, returning True
# to playGame, resulting in the loop in the calling function (main()) to continue.
def play():
playerChoice = int(playerMenu())
if playerChoice == 4:
return 0
else:
pcChoice = pcGenerate()
outcome = evaluateGame(playerChoice, pcChoice)
updateScoreBoard(outcome)
return 1
# prints the menu, the player selects a menu item, the input is validated, if the input is valid, returned the input, if
# the input is not valid, continue to prompt for a valid input
# 1 - rock
# 2 - paper
# 3 - scissors
# 4 - quit
def playerMenu():
print("Select a choice: \n [1]: Rock \n [2]: Paper \n [3]: Scissors \n [4]: Quit")
menuSelect = input("Enter menu selection: ")
while not validateInput(menuSelect):
invalidChoice(menuSelect)
menuSelect = input("Enter a correct value: ")
return menuSelect
# if the user doesn't input a 1-4 then return false, resulting in prompting the user for another value. If the value
# is valid, return True
# takes 1 argument
# menuSelection - value user entered prior
def validateInput(menuSelection):
if menuSelection == "1" or menuSelection == "2" or menuSelection == "3" or menuSelection == "4":
```
import random
tie = 0
pcWon = 0
playerWon = 0
# Displays program information, starts main play loop, after main loop is executed (user pressing 4 in menu), display
# the scores to the user.
def main():
print("A game or rock, paper, or scissors!")
playGame = True
while playGame:
playGame = play()
displayScoreBoard()
prompt = input("press enter to exit")
# displays the menu for user, if input ==4, playGame in the calling function (main()) is False, terminating the program.
# Generate a random int 1-3, evaluate the user input with the computer input, update globals accordingly, returning True
# to playGame, resulting in the loop in the calling function (main()) to continue.
def play():
playerChoice = int(playerMenu())
if playerChoice == 4:
return 0
else:
pcChoice = pcGenerate()
outcome = evaluateGame(playerChoice, pcChoice)
updateScoreBoard(outcome)
return 1
# prints the menu, the player selects a menu item, the input is validated, if the input is valid, returned the input, if
# the input is not valid, continue to prompt for a valid input
# 1 - rock
# 2 - paper
# 3 - scissors
# 4 - quit
def playerMenu():
print("Select a choice: \n [1]: Rock \n [2]: Paper \n [3]: Scissors \n [4]: Quit")
menuSelect = input("Enter menu selection: ")
while not validateInput(menuSelect):
invalidChoice(menuSelect)
menuSelect = input("Enter a correct value: ")
return menuSelect
# if the user doesn't input a 1-4 then return false, resulting in prompting the user for another value. If the value
# is valid, return True
# takes 1 argument
# menuSelection - value user entered prior
def validateInput(menuSelection):
if menuSelection == "1" or menuSelection == "2" or menuSelection == "3" or menuSelection == "4":
Solution
def playerMenu():
print('Select a choice: \n\n\t [1]: Rock \n\t [2]: Paper \n\t [3]: Scissors \n\t [4]: Quit\n')
menuSelect = input('Enter menu selection: ')
while menuSelect not in ['1','2','3','4']:
print(menuSelect, 'is not a valid option. Please use 1-4')
menuSelect = input('Enter a correct value: ')
return menuSelectNote I got rid of two unnecessary functions, validateInput and invalidChoice. Doing so increases performance and readability. However, don't play code golf. Separating sub-tasks and having uniform levels of abstraction is important. But, these were one line a piece.
The in keyword makes things simple, faster, and easier to read.
I will say also, you could use a dictionary to match the menuSelect to the corresponding option, ie. rock, paper, or scissors. I'd set that up in player menu upon their selection..
options = {1: 'rock', 2: 'paper', 3: 'scissors'}Then you can get rid of all those if statements. It should be a warning sign whenever there are too many if statements.
You only need to test if the player's option equals the computer's option once. But you test it three times.
You could use code like:
if playerChoice == pcChoice:
print("It's a tie! The computer has chosen", pcChoice, "as well!")
elif playerChoice == rock and pcChoice == scissors or
playerChoice == paper and pcChoice == rock:
print('You win! The computer has chosen', pcChoice + '.', playerChoice, 'beats', pcChoice +'!')
else:
print('You lose.. The computer has chosen', pcChoice + '.', pcChoice, 'beats', playerChoice +'!')There's probably some more, but I ran out of time
Code Snippets
def playerMenu():
print('Select a choice: \n\n\t [1]: Rock \n\t [2]: Paper \n\t [3]: Scissors \n\t [4]: Quit\n')
menuSelect = input('Enter menu selection: ')
while menuSelect not in ['1','2','3','4']:
print(menuSelect, 'is not a valid option. Please use 1-4')
menuSelect = input('Enter a correct value: ')
return menuSelectoptions = {1: 'rock', 2: 'paper', 3: 'scissors'}if playerChoice == pcChoice:
print("It's a tie! The computer has chosen", pcChoice, "as well!")
elif playerChoice == rock and pcChoice == scissors or
playerChoice == paper and pcChoice == rock:
print('You win! The computer has chosen', pcChoice + '.', playerChoice, 'beats', pcChoice +'!')
else:
print('You lose.. The computer has chosen', pcChoice + '.', pcChoice, 'beats', playerChoice +'!')Context
StackExchange Code Review Q#32779, answer score: 4
Revisions (0)
No revisions yet.