|
| 1 | +import random |
| 2 | + |
| 3 | +print('Winning rules of the game ROCK PAPER SCISSORS are :\n' |
| 4 | + + "Rock vs Paper -> Paper wins \n" |
| 5 | + + "Rock vs Scissors -> Rock wins \n" |
| 6 | + + "Paper vs Scissors -> Scissor wins \n") |
| 7 | + |
| 8 | +while True: |
| 9 | + print("Enter your choice \n 1 - Rock \n 2 - Paper \n 3 - Scissors \n") |
| 10 | + |
| 11 | + choice = int(input("Enter your choice: ")) |
| 12 | + |
| 13 | + # looping until the user enters a valid input |
| 14 | + while choice > 3 or choice < 1: |
| 15 | + choice = int(input('Enter a valid choice please: ')) |
| 16 | + |
| 17 | + # initialize the value of choice_name variable |
| 18 | + # corresponding to the choice value |
| 19 | + if choice == 1: |
| 20 | + choice_name = 'Rock' |
| 21 | + elif choice == 2: |
| 22 | + choice_name = 'Paper' |
| 23 | + else: |
| 24 | + choice_name = 'Scissors' |
| 25 | + |
| 26 | + # print the user's choice |
| 27 | + print('User choice is:', choice_name) |
| 28 | + print('Now it\'s the Computer\'s Turn....') |
| 29 | + |
| 30 | + # Computer chooses randomly any number |
| 31 | + # among 1, 2, and 3 using randint method |
| 32 | + comp_choice = random.randint(1, 3) |
| 33 | + |
| 34 | + # looping until comp_choice value |
| 35 | + # is equal to the choice value |
| 36 | + while comp_choice == choice: |
| 37 | + comp_choice = random.randint(1, 3) |
| 38 | + |
| 39 | + # initialize the value of comp_choice_name |
| 40 | + # variable corresponding to the choice value |
| 41 | + if comp_choice == 1: |
| 42 | + comp_choice_name = 'Rock' |
| 43 | + elif comp_choice == 2: |
| 44 | + comp_choice_name = 'Paper' |
| 45 | + else: |
| 46 | + comp_choice_name = 'Scissors' |
| 47 | + |
| 48 | + print("Computer choice is:", comp_choice_name) |
| 49 | + print(choice_name, 'Vs', comp_choice_name) |
| 50 | + |
| 51 | + # we need to check for a draw |
| 52 | + if choice == comp_choice: |
| 53 | + print('It\'s a Draw', end="") |
| 54 | + result = "DRAW" |
| 55 | + |
| 56 | + # condition for winning |
| 57 | + if ((choice == 1 and comp_choice == 2) or |
| 58 | + (choice == 2 and comp_choice == 3) or |
| 59 | + (choice == 3 and comp_choice == 1)): |
| 60 | + print('Computer wins =>', end="") |
| 61 | + result = 'Computer' |
| 62 | + else: |
| 63 | + print('You win =>', end="") |
| 64 | + result = 'User' |
| 65 | + |
| 66 | + # Printing either user or computer wins or draw |
| 67 | + if result == 'DRAW': |
| 68 | + print("<== It's a tie ==>") |
| 69 | + elif result == 'User': |
| 70 | + print("<== You win ==>") |
| 71 | + else: |
| 72 | + print("<== Computer wins ==>") |
| 73 | + |
| 74 | + print("Do you want to play again? (Y/N)") |
| 75 | + |
| 76 | + # if user input n or N, then the condition is True |
| 77 | + ans = input().lower() |
| 78 | + if ans == 'n': |
| 79 | + break |
| 80 | + |
| 81 | +# after coming out of the while loop |
| 82 | +# we print thanks for playing |
| 83 | +print("Thanks for playing") |
0 commit comments