This classic can be recreated in Python using if-statements and random module to get computer's choice. There are 9 situations possible in Rock-Paper-Scissors. Each situation can be recreated using an if-statement. Also, usage of numbers makes the job simpler and the numbers can later be converted to their respective game-equivalent. Using for-loop, we can play multiple rounds and dictionaries can help keep a record of number of wins.
CODE :
Click this link for the code: Rock Paper Scissors With Python
CODE :
Click this link for the code: Rock Paper Scissors With Python
def rps(): import random import time scores = {'You':0,'Computer':0} n = int(input('How many rounds do you want to play: ')) for x in range(n): print('\nRound - ',x+1) time.sleep(2) #For simplicity, lets assume 1 is rock, 2 is paper and 3 is scissors choices = ['Rock','Paper','Scissors'] while True: userchoice = int(input("\nEnter 1 for Rock, 2 for Paper and 3 for Scissors: ")) if userchoice not in [1,2,3]: print("\nThat is an invalid input\n") else: break cpuchoice = random.choice([1,2,3]) if userchoice == cpuchoice: print(f"\nDraw, You put {choices[userchoice-1]} and Computer put {choices[cpuchoice-1]}\n") elif userchoice == 1 and cpuchoice == 2: print(f"\nComputer Wins, You put {choices[userchoice-1]} and Computer put {choices[cpuchoice-1]}\n") scores['Computer'] += 1 elif userchoice == 1 and cpuchoice == 3: print(f"\nYou Win, You put {choices[userchoice-1]} and Computer put {choices[cpuchoice-1]}\n") scores['You'] += 1 elif userchoice == 2 and cpuchoice == 1: print(f"\nYou Win, You put {choices[userchoice-1]} and Computer put {choices[cpuchoice-1]}\n") scores['You'] += 1 elif userchoice == 2 and cpuchoice == 3: print(f"\nComputer Wins, You put {choices[userchoice-1]} and Computer put {choices[cpuchoice-1]}\n") scores['Computer'] += 1 elif userchoice == 3 and cpuchoice == 1: print(f"\nComputer Wins, You put {choices[userchoice-1]} and Computer put {choices[cpuchoice-1]}\n") scores['Computer'] += 1 elif userchoice == 3 and cpuchoice == 2: print(f"\nYou Win, You put {choices[userchoice-1]} and Computer put {choices[cpuchoice-1]}\n") scores['You'] += 1 time.sleep(2) print(f"\nFINAL SCORES:\n\nYou: {scores['You']}\nComputer: {scores['Computer']}") time.sleep(2) input('\n\nPress Enter to Exit') rps()
There are many possible outputs so, here's one of the many possible outputs:
How many rounds do you want to play: 2 Round - 1 Enter 1 for Rock, 2 for Paper and 3 for Scissors: 1 You Win, You put Rock and Computer put Scissors Round - 2 Enter 1 for Rock, 2 for Paper and 3 for Scissors: 3 Computer Wins, You put Scissors and Computer put Rock FINAL SCORES: You: 1 Computer: 1 Press Enter to Exit >>>
This is just the code for the basic game. With similar if-statements, the game can be expanded to Rock-Paper-Scissors-Lizard-Spock or any other updated editions. Let me know your version in the comment section.
If you want to check out other games with Python: PYTHON GAMES COMPILATION (thepygrammer.blogspot.com).
Comments
Post a Comment