0

So i wrote my first Rock, Paper, Scissors in Python and it nearly works. The problem is that it sometimes reads "scissors" as an invalid input, which seems to me completely randomly and i dont know what the problem is. Hoping for an easy fix for it. Here's the code:

import time
import random
choice_list = ["Rock", "Paper", "Scissors"]

def game():
    won = False
    lost = False
    while not won and not lost:
        user = input("Enter your choice from Rock, Paper or Scissors: ")
        user_i = user.lower()
        com_1 = random.choice(choice_list)
        com_choice = com_1.lower()
        if com_choice == user_i:
            print("Draw, try again!")
        elif com_choice == "rock" and user_i == "paper":
            won = True
        elif com_choice == "rock" and user_i == "scissors":
            lost = True
        elif com_choice == "paper" and user_i == "rock":
            lost = True
        elif com_choice == "paper" and user_i == "scissors":
            won = True
        elif com_choice == "scissors" and user_i == "rock":
            won = True
        elif com_choice == "scissors" and user_i == "paper":
            lost = True
        elif user != "scissors" or "paper" or "rock":
            print("Invalid choice, try again...")
    if won:
        print("Congrats you won, the PC chose " + com_1 + "!")
        won = False
    if lost:
        print("You lost, the PC chose " + com_1 + "!")
        lost = False

play_again = "yes"
while play_again.lower() == "yes":
    game()
    play_again = input("Would u like to play again? ")
if play_again.lower() != "yes":
    print("Goodbye!")
    time.sleep(3)

2 Answers2

1

The fix:

elif user_i not in ("scissors" or "paper" or "rock"):
    print("Invalid choice, try again...")

You were using user instead of user_i. So, when user is Scissors, the elif condition will fail("Scissors" != "scissors") and return Invalid choice.

Also, elif user != "scissors" or "paper" or "rock" is evaluated as
elif (user != 'scissors') or 'paper' or 'rock' which is not exactly what you intend although it doesn't affect your results. Instead, you should write

elif user_i not in ("scissors", "paper", "rock"):

Actually, you don't even need the last elif since you have already checked for all the valid inputs in the previous if-else statements.

Shiva
  • 2,627
  • 21
  • 33
0

In your program, when validating input for invalid words, you are using raw input user. This could have different case from user_i = user.lower().

Try with the below line:

elif ((user_i != "scissors") and (user_i != "paper") and (user_i != "rock")):
    print("Invalid choice, try again...")

If still you get issues, pls let us know your input.

Shiva
  • 2,627
  • 21
  • 33