Created
January 31, 2019 20:30
-
-
Save asafshkalim/58f0fdec7430c808c7afe1ff9e4432ee to your computer and use it in GitHub Desktop.
Rock Paper Scissors game (Python)
This file contains 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
human_score = 0 | |
computer_score = 0 | |
def choice_to_number(x): | |
return {'rock':1,'paper':2,'scissors':3}[x] | |
#print(mydict[name]) | |
def random_choice(): | |
import random | |
return random.choice(['rock','paper','scissors']) | |
def number_to_choice(random_choice): | |
return {1:'rock',2:'paper',3:'scissors'}[random_choice] | |
def choice_result(human_choice, computer_choice): | |
import time | |
time.sleep(1) | |
print('rock...') | |
time.sleep(1) | |
print('paper...') | |
time.sleep(1) | |
print('scissors...') | |
print('SHOOT!') | |
time.sleep(1) | |
if (human_choice == 1 and computer_choice == 2) or (human_choice == 2 and computer_choice == 3) or (human_choice == 3 and computer_choice == 1): | |
global computer_score | |
computer_score +=1 | |
print('You chose {} and the computer chose {}. You lose, good day sir!'.format(choice.upper(), computer_choice_n.upper())) | |
elif (human_choice == 2 and computer_choice ==1) or (human_choice == 3 and computer_choice == 2) or (human_choice == 1 and computer_choice == 3): | |
global human_score | |
human_score +=1 | |
print('You chose {} and the computer chose {}. You win!'.format(choice.upper(), computer_choice_n.upper())) | |
else: | |
print('You chose {} and the computer chose {}. It is a tie'.format(choice.upper(), computer_choice_n.upper())) | |
###GAME STARTS HERE | |
x = True | |
while x == True: | |
choice = input("Make your choice: rock, paper, or scissors? ") | |
if choice not in ['rock','paper','scissors']: | |
print('That is not an available option. Please try again.') | |
continue | |
human_choice = choice_to_number(choice) ## Converts the user's input into a number | |
computer_choice = choice_to_number(random_choice()) ##Takes a randomly generated choice, and converts it a number | |
computer_choice_n = number_to_choice(computer_choice) ##This is the randomly generated choice in name form. Used for returning text | |
choice_result(human_choice, computer_choice) ##compares values, spits out result, tallies the score | |
print('\n') | |
print("Your wins: " + str(human_score)) | |
print("Computers wins: " + str(computer_score)) | |
print('\n') | |
if input("Do you want to play again? ")[0].lower() == 'y': | |
continue | |
else: | |
x = False | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment