Code Review for Paper, Rock, Scissors

Revenant faceofoblivionofficial at gmail.com
Tue Oct 14 04:04:38 EDT 2014


Hi all!

I'm new to Python and programming in general, and am trying to learn as much as I can about it. 

Anyway, for a basic first program I made a simple game of Paper, Rock, Scissors. For this program, I incorporated a main menu that presented three different options, allowed the user to play a game of Paper, Rock, Scissors, allowed them to play the game again, and most importantly checked the user's input to make sure the program doesn't fail due to errors. 

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. 

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): ')
    # Checks for an invalid selection.
    while menuselection != 'Play' and menuselection != 'play' and menuselection != 'Help' and menuselection != 'help' \
            and menuselection != 'About' and menuselection != '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()


# 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':
        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)
    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.')
    # 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()


# 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()
        else:
            print('\nThanks for playing!')


# 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()



More information about the Python-list mailing list