|
| 1 | +import random |
| 2 | + |
| 3 | + |
| 4 | +def rock_paper_scissor(userPoint, comPoint, chances): |
| 5 | + # This while loop continue to till chances is leaser than |
| 6 | + while(chances < 10): |
| 7 | + randomChoice = random.choice(lst) |
| 8 | + userGuess = str(input("Rock, Paper, scissors:\n")) |
| 9 | + # Computer wins condition |
| 10 | + if userGuess.lower() == "r" and randomChoice == "paper": |
| 11 | + print(f"Computer win!\n{'-'*46}\n") |
| 12 | + comPoint += 1 |
| 13 | + chances += 1 |
| 14 | + elif userGuess.lower() == "p" and randomChoice == "scissors": |
| 15 | + print(f"Computer win!\n{'-'*46}\n") |
| 16 | + comPoint += 1 |
| 17 | + chances += 1 |
| 18 | + elif userGuess.lower() == "s" and randomChoice == "rock": |
| 19 | + print(f"Computer win!\n{'-'*46}\n") |
| 20 | + comPoint += 1 |
| 21 | + chances += 1 |
| 22 | + # User wins condition |
| 23 | + elif randomChoice == "rock" and userGuess.lower() == "p": |
| 24 | + print(f"You win!\n{'-'*46}\n") |
| 25 | + userPoint += 1 |
| 26 | + chances += 1 |
| 27 | + elif randomChoice == "paper" and userGuess.lower() == "s": |
| 28 | + print(f"You win!\n{'-'*46}\n") |
| 29 | + userPoint += 1 |
| 30 | + chances += 1 |
| 31 | + elif randomChoice == "scissors" and userGuess.lower() == "r": |
| 32 | + print(f"You win!\n{'-'*46}\n") |
| 33 | + userPoint += 1 |
| 34 | + chances += 1 |
| 35 | + # Draw situation |
| 36 | + elif (randomChoice == "rock" and userGuess == "r") or (randomChoice == "paper" and userGuess == "p") or (randomChoice == "scissors" and userGuess == "s"): |
| 37 | + print(f"It's a tie!\n{'-'*46}\n") |
| 38 | + chances += 1 |
| 39 | + |
| 40 | + if userPoint > comPoint: |
| 41 | + print( |
| 42 | + f"#. User Point is: {userPoint}\n#. Computer Point is: {comPoint}\n") |
| 43 | + print(f"You won!! this game.") |
| 44 | + elif comPoint > userPoint: |
| 45 | + print( |
| 46 | + f"#. User Point is: {userPoint}\n#. Computer Point is: {comPoint}\n") |
| 47 | + print(f"Computer won!! this game.\n") |
| 48 | + else: |
| 49 | + print( |
| 50 | + f"#. User Point is: {userPoint}\n#. Computer Point is: {comPoint}\n") |
| 51 | + print("#. It's tie!") |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + # This loop is for if user wants to play again or wants to exit the game |
| 56 | + while(True): |
| 57 | + print("1. Press 'r' for rock\n2. Press 'p' for paper\n3. Press 's' forscissors\n") |
| 58 | + lst = ["rock", "paper", "scissors"] |
| 59 | + chances = 0 |
| 60 | + comPoint = 0 |
| 61 | + userPoint = 0 |
| 62 | + rock_paper_scissor(userPoint, comPoint, chances) |
| 63 | + |
| 64 | + ask = input("Press any key to continue and e to exit:\n") |
| 65 | + if ask.lower() == 'e': |
| 66 | + break |
0 commit comments