Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exercise 1.7 – Rock, Paper, Scissors
- # function to keep nagging to get a good choice
- def get_player_choice(player_name):
- bad_choice = True
- good_choices = ["rock", "paper", "scissors"]
- while bad_choice:
- choice = raw_input(player_name + ": rock, paper or scissors? ")
- if choice in good_choices:
- bad_choice = False
- return choice
- else:
- print "This is not a valid object selection\n"
- bad_choice = True
- # function to compare the choices
- def check_outcome(choice1, choice2):
- print choice1, choice2
- if choice1 == choice2:
- return "Tie!"
- elif choice1 == 'rock':
- if choice2 == 'scissors':
- return "Player 1 wins"
- else:
- return "Player 2 wins"
- elif choice1 == 'paper':
- if choice2 == 'rock':
- return "Player 1 wins"
- else:
- return "Player 2 wins"
- elif choice1 == 'scissors':
- if choice2 == 'rock':
- return "Player 2 wins"
- else:
- return "Player 1 wins"
- else:
- return "I'm confused"
- # main part of the program
- play_again = True
- while play_again:
- play_again = raw_input("\nReady to play? (y/n) ").lower()
- if play_again in ["y", "yes"]:
- play_again = True
- player1 = get_player_choice("Player1")
- player2 = get_player_choice("Player2")
- print check_outcome(player1, player2)
- else:
- play_again = False
- print "See you next time!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement