Last active
November 26, 2017 08:13
-
-
Save chaewonkong/fc67e834b3e7fc776617c1c2bdc94a4a to your computer and use it in GitHub Desktop.
Rocks Sissors Papers Game program with 'Try Again' for draw situation. After playing, it asks yes no question to play again or not.
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
| #This is an python game of 'Rocks, Sissors, Papers' | |
| #A User can compete with the computer by select one of 'r', 's', 'p', which indicates 'Rock', 'Sissor', and 'Paper' | |
| #Computer, decides one of 'Rock', 'Sissor', 'Paper' as well, by using random.choice | |
| #After playing one game, the program will ask you whether want to play another game or not. | |
| import random | |
| #Function that changes input of ['r', 's', 'p'] as output of ['Rock', 'Sissor', 'Paper'] | |
| def rsp_func(user): | |
| if user == 'r': | |
| return 'Rock' | |
| elif user == 's': | |
| return 'Sissor' | |
| else: | |
| return 'Paper' | |
| #To make regame possible. IF game=True, it means you play this game again and again. | |
| #If it is False, it will print "Good Bye~" and you can quit the game. | |
| game=True | |
| while game: | |
| print("\nWelcome to Rocks Sissors Papers Game!\n") | |
| running=True | |
| while running: | |
| player = input("Press 'r' for 'Rock', 's' for 'Sissor', 'p' for 'Paper':") | |
| computer = random.choice(['r', 's', 'p']) | |
| #print 'Rock' for 'r', 'Sissor' for 's', 'Paper' for 'p' as Player's turn | |
| # print 'Rock' for 'r', 'Sissor' for 's', 'Paper' for 'p' as Computer's turn | |
| print('\nYou:', rsp_func(player)) | |
| print('Computer:', rsp_func(computer), '\n') | |
| # If the outcome is 'Draw' than play again. Else, move on to the next step. | |
| if player==computer: | |
| print('\nDraw! Try Again~\n') | |
| else: | |
| running=False | |
| #Rocks win over sissors, sissors win over papers, papers win over rocks. | |
| #In this step, player will be told whether he wins or loses. | |
| #Remember, There is only win-lose situations since draws lead to playing again automatically. | |
| if player=='r': | |
| if computer=='s': | |
| print('\nCongratulations~ You Win!\n') | |
| else: #computer=='p': | |
| print('\nYou lose!\n') | |
| elif player=='s': | |
| if computer=='r': | |
| print('\nYou lose!\n') | |
| else: #computer=='p': | |
| print('\nCongratulations~ You Win!\n') | |
| else: #player=='p': | |
| if computer=='r': | |
| print('\nCongratulations~ You Win!\n') | |
| else: #computer=='s': | |
| print('\nYou lose!\n') | |
| #Enquire the player whether he wants to play again or not. 'y' for 'yes', 'n' for 'no'. | |
| redo=input("\nDo you want to play again? press 'y' for 'Yes' and 'n' for 'No':") | |
| if redo=='y': | |
| print("\nYou chose 'Yes'. Let's play again!\n") | |
| else: | |
| print("\nGood bye~\n") | |
| game=False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment