Problem with input and if functions

Hey guys! I am on early stage with programing learning and I need help. I wrote them a possible cases to write and ansews on their. While first three with answer “gracz1” are fully working, that next three with answer “gracz2” aren’t. I tried to switched then for places and it wored, but it was the other way around.
Why is it like that?

The reason why is because the computer reads the code like this (I will just exchange the variables with “player1”, “player2”, “rock”, “paper” and “scissors” for my convenience)

if player1 == "paper"
True? -> if player2 == "rock"
                True? -> player1 wins
False? continue (player1 not paper)
if player1 == "scissors"
True? -> if player2 == "paper"
                True? -> player1 wins
False? continue (player1 not paper scissors)
if player1 == "rock"
True? -> if player2 == "scissors"
                True? -> player1 wins
False? continue (player1 not rock paper scissors)
if player1 == "scissors"
True? ... (will never be triggered, 
because the "else" (or here I used "False?") above already checked 
if player1 is scissors, now the situation is like "if it is not scissors, see if it is scissors" 
...

therefore, the remaining 3 comparisons will never be triggered because

to solve this, you can do nested if

if player1 == "paper":
  if player2 == "scissors":
    print('player2 wins')
  elif player2 == "rock":
    print('player1 wins')
elif player1 == 'scissors':
  ...

using ‘and’

if player1 == "paper" and player2 == "scissors":
    print('player2 wins')
elif player1 == 'paper' and player2 == "rock":
    print('player1 wins')
elif player1 == 'scissors' and player2 == 'rock':
  ...

no else

if player1 == "paper":
  if player2 == "scissors":
    print('player2 wins')
if player1 == 'paper':
  if player2 == "rock":
    print('player1 wins')
if player1 == 'scissors':
  ...
1 Like

OK, thanks so much :+1:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.