Created
May 20, 2019 00:26
-
-
Save jpwhite3/43037f283cb8b097ec7d19e33ac8006e to your computer and use it in GitHub Desktop.
"Rock-Paper-Scissors" in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
CHOICES = 'rps' | |
def get_player_choice(): | |
"""Get user input and validate it is one of the choices above""" | |
player_choice = None | |
while player_choice is None: | |
player_choice = input('Choices: \n(R)ock \n(P)aper \n(S)cissors \n\nPick: ') | |
if player_choice.lower() not in CHOICES: | |
player_choice = None | |
return player_choice.lower() | |
def get_computer_choice(): | |
"""Have the computer pick one of the valid choices at random""" | |
computer_choice = random.randint(0, 2) | |
computer_choice = CHOICES[computer_choice] | |
return computer_choice | |
def is_draw(player_choice, computer_choice): | |
"""Check if game was a draw""" | |
if player_choice == computer_choice: | |
return True | |
def print_winner(player_choice, computer_choice): | |
"""Check to see who won""" | |
if player_choice == 'r' and computer_choice == 's': | |
print('Player wins!') | |
elif player_choice == 's' and computer_choice == 'p': | |
print('Player wins!') | |
elif player_choice == 'p' and computer_choice == 'r': | |
print('Player wins!') | |
else: | |
print('Computer wins!') | |
print('%s beats %s' % (computer_choice, player_choice)) | |
def play_game(): | |
"""play the game""" | |
player_choice = get_player_choice() | |
computer_choice = get_computer_choice() | |
if is_draw(player_choice, computer_choice): | |
print("It's a draw, both players picked %s: " % player_choice) | |
else: | |
print("Computer picked: %s" % computer_choice) | |
print("Player picked: %s" % player_choice) | |
print_winner(player_choice, computer_choice) | |
if __name__ == "__main__": | |
play_game() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import random
comp_choise = ['rock' , 'papper' , 'scissor']
while True:
user_input = input(f'choose one {comp_choise} ')
if user_input in ['roc','ROCK','r']:
user_input = 'rock'
elif user_input in ['paper','p','pap']:
user_input = 'papper'
elif user_input in ['scisor', 's', 'scissors']:
user_input = f'{comp_choise()}'
else:
print(f'{user_input} is invalid try again ! ')
comp_action = random.choice(comp_choise)
if user_input == comp_choise:
print(f'ur input is {user_input} and comp is also {comp_action} , try ties again ! ')
elif(user_input == 'rock' and comp_action == 'scissor') or
(user_input == 'scissor' and comp_action == 'papper') or
(user_input == 'papper' and comp_action == 'rock') :
print(f'ur choose {user_input} beats {comp_action} . user win! ')
else:
print('comp {comp_action} beats {user_input} , comp is win ')
play_again = input("Play again? (y/n): ").lower()
if play_again != "y":
print("Thanks for playing!")
break