Code Review for Paper, Rock, Scissors

Dave Angel davea at davea.name
Tue Oct 14 12:23:47 CEST 2014


Revenant <faceofoblivionofficial at gmail.com> Wrote in message:
> 
> 
....
> One thing I want to try to add is a "Press any key to continue" function that occurs at the end of the program when the user decides to quit. I looked at some options online, but haven't quite figured it out yet. 
> 

Use the curses function getch, as documented on:
    https://docs.python.org/3/howto/curses.html

Or if you're on Windows, try msvcrt.getch.

If you're writing code that needs to be portable, look at:
    http://code.activestate.com/recipes/577977-get-single-keypress/


> As previously stated, I am new to Python and would also like to see if any of you programming gurus have some suggestions about how I can simplify code, and also if there are any other good starter programs to work on to improve my skills. 
> 
> Thanks for reading! Code is below:
> 
> 
> # Creates the main menu.
> def menu():
>     # Sets the scores to 0.
>     global playerscore
>     global compscore
>     global draws
>     playerscore = 0
>     compscore = 0
>     draws = 0
>     menuselection = input('Please enter a selection: (Play/Help/About): ')

Save yourself trouble by using the lower method on the result of
 each input function. I'll assume that for subsequent comments.
 

>     # Checks for an invalid selection.
>     while menuselection != 'Play' and menuselection != 'play' and menuselection != 'Help' and menuselection != 'help' \
>             and menuselection != 'About' and menuselection != 'about':

Simplify to:
    while menuselection not in ("play", "help", "about"):

>         print('You have entered an invalid selection.')
>         menuselection = input('\nPlease type a selection: (Play/Help/About): ')
>     else:
>         if menuselection == 'Play' or menuselection == 'play':
>             play()
>         elif menuselection == 'Help' or menuselection == 'help':
>             instructions()
>         else:
>             about()
> 

    func = {"play":play, "help":instructions, "about":about}
    func[menuselection]()
> 
> # Creates the game.
> def play():
>     global playerscore
>     global compscore
>     global draws
>     # Player chooses Paper, Rock, or Scissors.
>     playerselect = input('\nPlease choose Paper, Rock, or Scissors: ')
>     # Checks for an invalid selection.
>     while playerselect != 'Paper' and playerselect != 'paper' and playerselect != 'Rock' and playerselect != 'rock' \
>             and playerselect != 'Scissors' and playerselect != 'scissors':

Again use playerselect not in ("pap... form

>         print('You have entered an invalid selection.')
>         playerselect = input('\nPlease choose Paper, Rock, or Scissors: ')
>     else:
>         if playerselect == 'Paper' or playerselect == 'paper':
>             print('\nYou have selected Paper.')
>             playerselect = 1
>         elif playerselect == 'Rock' or playerselect == 'rock':
>             print('\nYou have selected Rock.')
>             playerselect = 2
>         else:
>             print('\nYou have selected Scissors.')
>             playerselect = 3
>     # Computer chooses Paper, Rock, or Scissors.
>     import random
>     compselect = random.randint(1, 3)

You could make life a little easier by using 0 to 2 instead,  here
and above.

>     if compselect == 1:
>         print('The Computer has selected Paper')
>     elif compselect == 2:
>         print('The Computer has selected Rock.')
>     else:
>         print('The Computer has selected Scissors.')

     print (None, "paper", "rock", "scissors")[compselect])

The next section can be simplified a lot by exploiting some
 symmetries. 
    diff = (playerselect - compselect ) % 3

This number will be zero for a draw, 1 for computer win, and 2 for
 player win. So you can have 3 cases below instead of
 9.


>     # Results if player selects paper.
>     if playerselect == 1 and compselect == 1:
>         print('Draw!')
>         draws += 1
>         score()
>     else:
>         if playerselect == 1 and compselect == 2:
>             print('Paper beats rock. You win!')
>             playerscore += 1
>             score()
>         elif playerselect == 1 and compselect == 3:
>             print('Paper is beaten by scissors. You lose!')
>             compscore += 1
>             score()
>     # Results if player selects rock.
>     if playerselect == 2 and compselect == 2:
>         print('Draw!')
>         draws += 1
>         score()
>     else:
>         if playerselect == 2 and compselect == 1:
>             print('Rock is beaten by paper. You lose!')
>             compscore += 1
>             score()
>         elif playerselect == 2 and compselect == 3:
>             print('Rock beats scissors. You win!')
>             playerscore += 1
>             score()
>     # Results if player selects rock.
>     if playerselect == 3 and compselect == 3:
>         print('Draw!')
>         draws += 1
>         score()
>     else:
>         if playerselect == 3 and compselect == 1:
>             print('Scissors beat paper. You win!')
>             playerscore += 1
>             score()
>         elif playerselect == 3 and compselect == 2:
>             print('Scissors are beaten by rock. You lose!')
>             compscore += 1
>             score()
>     again()

This makes me a little nervous. You have mutual recursion going on
 between functions play and again.  It probably won't matter here,
 but it's a bad habit to get into. 

What you really should do is something like

def play_once ():  (current body of play function,  but without
 ref to again)

def play ():
    while True:
        play_once ()
        if not again():
            break
And change again so it returns True or False

> 
> # Determines if the player wants to play another game.
> def again():
>     replay = input('\nDo you want to play again (Y/N)? ')
>     while replay != 'Y' and replay != 'y' and replay != 'N' and replay != 'n':
>         print('You have entered an invalid selection.')
>         replay = input('\nDo you want to play again (Y/N)? ')
>     else:
>         if replay == 'Y' or replay == 'y':
>             play()

Replace the call to play () with
              return True

>         else:
>             print('\nThanks for playing!')
> 
    return False

> 
> # Creates the instructions.
> def instructions():
>     print('\nPaper, Rock, Scissors is a simple game played against a computer opponent.')
>     print('The player will have a choice of selecting paper, rock, or scissors.')
>     print('The player\'s result will be compared with that of the computer to determine who wins the round.')
>     print('In the event that both the player and the computer have the same selection, the round will end in a tie.')
>     print('\nPaper beats rock but loses to scissors.')
>     print('\nRock beats scissors but loses to paper.')
>     print('\nScissors beats paper but loses to rock.')
>     print('\nGood luck, and have fun!\n')
>     menu()
> 
> 
> # Creates the about section.
> def about():
>     print('\nPaper, Rock, Scissors\n\nVersion 1.0\n\nCreated by <Name>, 07 October 2014\n')
>     menu()
> 
> 
> # Calculates score.
> def score():
>     print('\nCurrent Scores: ')
>     print('\nPlayer Score:', playerscore)
>     print('\nComputer Score:', compscore)
>     print('\nDraws:', draws)
> 
> 
> # Start of program operations.
> print('Welcome to Paper, Rock, Scissors!\n')
> menu()
> 


-- 
DaveA




More information about the Python-list mailing list