1

I'm not having much luck grouping multiple if conditional statements in python.

try:     
  player1_input = input("Enter 1 for Rock, 2 for Paper, 3 for Scissors: ")
  player2_input = input("Enter 1 for Rock, 2 for Paper, 3 for Scissors: ")

  if (player1_input or player2_input) != ("1" or "2" or "3"):
    raise
except Exception:
  print("You did not enter a correct input")

From what I want to achieve, I would like to raise an exception if user 1 or 2 do not enter and input that is 1,2 or 3. However, when I try a test input of user1: 3 and user2: 1 a exception is incorrectly raised. How can I fix this issue? Any help would be greatly appreciated. I do believe I must have a misunderstanding of the syntax of multi-conditional if statements.

Shadow_S
  • 11
  • 3
  • 2
    There is a dupe that always gets posted (because this is a common problem), and I always forget the link. Anyway, what you want is to define, say, `valid_input = ['1', '2', '3']`. Then, you can do `if player_1_input in valid_input and player_2_input in valid_input`. – gmds Apr 30 '19 at 00:03
  • Thank you. That is a good solution. However, I was wondering why the above my original method would not work within the realm of python? – Shadow_S Apr 30 '19 at 00:11
  • You could check [this](https://stackoverflow.com/questions/26960867/problems-understanding-conditions-in-if-statement-python?rq=1) out. – gmds Apr 30 '19 at 00:14
  • 1
    You're writing Python like it's English and the operators are English conjunctions. Python `or` is a boolean operator, not a conjunction. – user2357112 Apr 30 '19 at 00:15
  • 1
    @gmds: Or to test both without repeating `valid_input` (really only worth it if testing more than two inputs), `if all(inp in valid_input for inp in (player_1_input, player_2_input)):` – ShadowRanger Apr 30 '19 at 00:20

0 Answers0