HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

Stone, Paper, Scissors in Python

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scissorspaperpythonstone

Problem

Recently I started programming with python.
Today I was trying to make a stone, paper, scissor game. After so scratching my head long time, finally I got a working code.

```
from random import choice

def play_again():
print "Do you want to play again:- Choose 'yay' or 'nay'"
user_again = raw_input('->')
if user_again == 'yay':
SPCa()
elif user_again == 'nay':
print "Ok bye! hope you enjoyed the game. See you soon! :)"
else:
print "Please choose correct option."
play_again()

def SPCa():
computer_choice = choice( ['stone', 'paper', 'scissor'] )
computer_choosed = "Computer choosed %s" % computer_choice
print "Make an choice"
print "Choose stone, paper, scissor"
user_choice = raw_input('->')

if user_choice == computer_choice:
print computer_choosed
print "So it's a tie"
play_again()

elif user_choice == 'stone':
if computer_choice == 'paper':
print computer_choosed
print "So, You loose"
play_again()
elif computer_choice == 'scissor':
print computer_choosed
print "So, Cheers! You won!"
play_again()

elif user_choice == 'paper':
if computer_choice == 'scissor':
print computer_choosed
print "So, you loose."
play_again()
elif computer_choice == 'stone':
print computer_choosed
print "So, Cheers! You won!"
play_again()

elif user_choice == 'scissor':
if computer_choice == 'stone':
print computer_choosed
print "So, you loose."
play_again()
elif computer_choice == 'paper':
print computer_choosed
print "So, Cheers! You won!"
play_again()
else:
print "please choose correct option"
SPCa()

def SPC():
computer_choice = choice( ['stone', 'paper', '

Solution

Little issues

Your indentations seems wrong : too many spaces in a few places, not enough in a few other places. Whitespaces matter in Python so this is definitly something you should fix before going any further.

Style

Python has a style guide called PEP 8 which is definitly worth reading and and worth following if you do not have good reasons not to. In you case, your usage of whitespaces around parenthesis and the trailing whitespaces for instance are not compliant to PEP8. You'll find tools online to check your code compliancy to PEP8 in a automated way if you want to. This could also help you to detect and fix your indentation issues.

Don't Repeat Yourself

You've already realised that your code was repeating itself and that is was a bad thing. Let's see how this can be improved.

The only difference between SPCa and SPC is that SPC has an additional line printed at the beginning. It might be easier to replace all the calls to SPC() by a call to print and then a call to SPCa. Once this is done, we can get rid of SPC and maybe rename SPCa in play_game.

At this stage, the code looks like:

from random import choice

def play_again():
    print "Do you want to play again:- Choose 'yay' or 'nay'"
    user_again = raw_input('->')
    if user_again == 'yay':
        play_game()
    elif user_again == 'nay':
        print "Ok bye! hope you enjoyed the game. See you soon! :)"
    else:
        print "Please choose correct option."
        play_again()

def play_game():
    computer_choice = choice(['stone', 'paper', 'scissor'])
    computer_choosed = "Computer choosed %s" % computer_choice
    print "Make an choice"
    print "Choose stone, paper, scissor"
    user_choice = raw_input('->')

    if user_choice == computer_choice:
        print computer_choosed
        print "So it's a tie"
        play_again()

    elif user_choice == 'stone':
        if computer_choice == 'paper':
            print computer_choosed
            print "So, You loose"
            play_again()
        elif computer_choice == 'scissor':
            print computer_choosed
            print "So, Cheers! You won!"
            play_again()

    elif user_choice == 'paper':
        if computer_choice == 'scissor':
            print computer_choosed
            print "So, you loose."
            play_again()
        elif computer_choice == 'stone':
            print computer_choosed
            print "So, Cheers! You won!"
            play_again()

    elif user_choice == 'scissor':
        if computer_choice == 'stone':
            print computer_choosed
            print "So, you loose."
            play_again()
        elif computer_choice == 'paper':
            print computer_choosed
            print "So, Cheers! You won!"
            play_again()
    else:
        print "please choose correct option"
        play_game()

print "You are playing Stone, Paper, Scissor."
play_game()


if main guard

In Python, it is a good habit to move your code actually doing things (by opposition to merely defining things) behind an if __name__ == "__main__": guard. This is useful if you want to reuse the code : you can import the file and get all the benefits from it (the definition of values/functions/classes) without having it performing unwanted actions.

In your code, the end of the script becomes :

if __name__ == "__main__":
    # execute only if run as a script
    print "You are playing Stone, Paper, Scissor."
    play_game()


Now we can get into the actual changes in your code. One of the issue it that you have multiple functions calling each other which make things difficult to understand.

All branches in play_game end up calling play_again (except when the option is not correct). It may be easier to call it once, at the end of the function like this.

else:
    print "please choose correct option"
    play_game()
    return
play_again()


However, an even more simple option would be to check at the beginnign that the value is correct. You could define a list with the correct options and use it like this :

```
from random import choice

game_options = ['stone', 'paper', 'scissor']

def play_again():
print "Do you want to play again:- Choose 'yay' or 'nay'"
user_again = raw_input('->')
if user_again == 'yay':
play_game()
elif user_again == 'nay':
print "Ok bye! hope you enjoyed the game. See you soon! :)"
else:
print "Please choose correct option."
play_again()

def play_game():
computer_choice = choice(game_options)
computer_choosed = "Computer choosed %s" % computer_choice
print "Make an choice"
print "Choose stone, paper, scissor"
user_choice = raw_input('->')

if user_choice not in game_options:
print "please choose correct option"
play_game()
return

if user_choice == computer_choice:
print computer_choosed
print "So it's a tie"

elif user_choice == 'stone':

Code Snippets

from random import choice


def play_again():
    print "Do you want to play again:- Choose 'yay' or 'nay'"
    user_again = raw_input('->')
    if user_again == 'yay':
        play_game()
    elif user_again == 'nay':
        print "Ok bye! hope you enjoyed the game. See you soon! :)"
    else:
        print "Please choose correct option."
        play_again()


def play_game():
    computer_choice = choice(['stone', 'paper', 'scissor'])
    computer_choosed = "Computer choosed %s" % computer_choice
    print "Make an choice"
    print "Choose stone, paper, scissor"
    user_choice = raw_input('->')

    if user_choice == computer_choice:
        print computer_choosed
        print "So it's a tie"
        play_again()

    elif user_choice == 'stone':
        if computer_choice == 'paper':
            print computer_choosed
            print "So, You loose"
            play_again()
        elif computer_choice == 'scissor':
            print computer_choosed
            print "So, Cheers! You won!"
            play_again()

    elif user_choice == 'paper':
        if computer_choice == 'scissor':
            print computer_choosed
            print "So, you loose."
            play_again()
        elif computer_choice == 'stone':
            print computer_choosed
            print "So, Cheers! You won!"
            play_again()

    elif user_choice == 'scissor':
        if computer_choice == 'stone':
            print computer_choosed
            print "So, you loose."
            play_again()
        elif computer_choice == 'paper':
            print computer_choosed
            print "So, Cheers! You won!"
            play_again()
    else:
        print "please choose correct option"
        play_game()


print "You are playing Stone, Paper, Scissor."
play_game()
if __name__ == "__main__":
    # execute only if run as a script
    print "You are playing Stone, Paper, Scissor."
    play_game()
else:
    print "please choose correct option"
    play_game()
    return
play_again()
from random import choice

game_options = ['stone', 'paper', 'scissor']


def play_again():
    print "Do you want to play again:- Choose 'yay' or 'nay'"
    user_again = raw_input('->')
    if user_again == 'yay':
        play_game()
    elif user_again == 'nay':
        print "Ok bye! hope you enjoyed the game. See you soon! :)"
    else:
        print "Please choose correct option."
        play_again()


def play_game():
    computer_choice = choice(game_options)
    computer_choosed = "Computer choosed %s" % computer_choice
    print "Make an choice"
    print "Choose stone, paper, scissor"
    user_choice = raw_input('->')

    if user_choice not in game_options:
        print "please choose correct option"
        play_game()
        return

    if user_choice == computer_choice:
        print computer_choosed
        print "So it's a tie"

    elif user_choice == 'stone':
        if computer_choice == 'paper':
            print computer_choosed
            print "So, You loose"
        elif computer_choice == 'scissor':
            print computer_choosed
            print "So, Cheers! You won!"

    elif user_choice == 'paper':
        if computer_choice == 'scissor':
            print computer_choosed
            print "So, you loose."
        elif computer_choice == 'stone':
            print computer_choosed
            print "So, Cheers! You won!"

    elif user_choice == 'scissor':
        if computer_choice == 'stone':
            print computer_choosed
            print "So, you loose."
        elif computer_choice == 'paper':
            print computer_choosed
            print "So, Cheers! You won!"

    play_again()


if __name__ == "__main__":
    # execute only if run as a script
    print "You are playing Stone, Paper, Scissor."
    play_game()
from random import choice

game_options = ['stone', 'paper', 'scissor']

def get_user_input_in_list(lst):
    user_input = raw_input('->')
    while True:
        if user_input in lst:
            return user_input
        else:
            print "Please choose correct option."

def play_again():
    print "Do you want to play again:- Choose 'yay' or 'nay'"
    user_again = get_user_input_in_list(['yay', 'nay'])
    if user_again == 'yay':
        play_game()
    elif user_again == 'nay':
        print "Ok bye! hope you enjoyed the game. See you soon! :)"


def play_game():
    computer_choice = choice(game_options)
    computer_choosed = "Computer choosed %s" % computer_choice
    print "Make an choice"
    print "Choose stone, paper, scissor"
    user_choice = get_user_input_in_list(game_options)

    if user_choice == computer_choice:
        print computer_choosed
        print "So it's a tie"

    elif user_choice == 'stone':
        if computer_choice == 'paper':
            print computer_choosed
            print "So, You loose"
        elif computer_choice == 'scissor':
            print computer_choosed
            print "So, Cheers! You won!"

    elif user_choice == 'paper':
        if computer_choice == 'scissor':
            print computer_choosed
            print "So, you loose."
        elif computer_choice == 'stone':
            print computer_choosed
            print "So, Cheers! You won!"

    elif user_choice == 'scissor':
        if computer_choice == 'stone':
            print computer_choosed
            print "So, you loose."
        elif computer_choice == 'paper':
            print computer_choosed
            print "So, Cheers! You won!"

    play_again()


if __name__ == "__main__":
    # execute only if run as a script
    print "You are playing Stone, Paper, Scissor."
    play_game()

Context

StackExchange Code Review Q#134711, answer score: 18

Revisions (0)

No revisions yet.