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':
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()
Also, this may call for a better option. You could define a function asking a user for a value in a list. This function could be used in 2 places and make your code easier to follow and less repetitive.
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()
This is better but we still have play_game
calling play_again
and play_again
calling play_game
.
Maybe play_game
should be used to play a single game and shouldn't call play_again
at all. This can be done by removing the call to play_again
in play_game
and moving it after the call to play_game
in play_again
. That way, we'd just have play_game
calling itself.
You'd have something like :
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()
play_again()
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!"
if __name__ == "__main__":
# execute only if run as a script
print "You are playing Stone, Paper, Scissor."
play_game()
play_again()
This is a bit better but you can go further in the separation of concerns. It is probably a better option to have play_again
to return a boolean and have a while
loop ensuring we call play_game
as long as required.
This would look like:
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'])
return user_again == 'yay'
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!"
if __name__ == "__main__":
# execute only if run as a script
print "You are playing Stone, Paper, Scissor."
while True:
play_game()
if not play_again():
print "Ok bye! hope you enjoyed the game. See you soon! :)"
break
Now we can get into the internals of play_game
.
First, you could get rid of the various way where printing is repeated.
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)
print computer_choosed
if user_choice == computer_choice:
assert result == 0
print "So it's a tie"
else:
win = False # unused value
if user_choice == 'stone':
win = (computer_choice == 'scissor')
elif user_choice == 'paper':
win = (computer_choice == 'stone')
elif user_choice == 'scissor':
win = (computer_choice == 'paper')
if win:
print "So, Cheers! You won!"
else:
print "So, you loose."
There are various way to define who wins in a game of player/scissors/rock. You could define a dictionnary mapping the different combinations possible to the winner. I quite like using modulo arithmetic to find the result.
Final code looks like:
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'])
return user_again == 'yay'
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 = 'stone' # get_user_input_in_list(game_options)
computer_idx = game_options.index(computer_choice)
user_idx = game_options.index(user_choice)
result = (computer_idx - user_idx) % 3
print computer_choosed
if result == 0:
print "So it's a tie"
elif result == 1:
print "So, you loose."
else:
assert result == 2
print "So, Cheers! You won!"
if __name__ == "__main__":
# execute only if run as a script
print "You are playing Stone, Paper, Scissor."
while True:
play_game()
if not play_again():
print "Ok bye! hope you enjoyed the game. See you soon! :)"
break
to choose
ischose
and notchoosed
. It isa choice
, notan choice
. \$\endgroup\$ – Graipher Jul 13 '16 at 9:02if
loops \$\endgroup\$ – vakus Jul 13 '16 at 9:36