0

i know this question may sound stupid, but i´m having problems to check/compare Strings in an if-Statement.

I´m new to Python and we need to make a little project for our school work in python. I decided to do "Rock, Paper, Scissors" as an Console Application.

The Problem i am facing, is that i can´t really compare the User-Input with strings in an if-Statement. I already tried different versions for ex.

Benutzerwahl = input("Wähle aus: Schere, Stein, Papier:")
if not Benutzerwahl == "Schere" or Benutzerwahl == "Stein" or Benutzerwahl == "Papier":
    print ("\n")
    print ("Wrong Input, please type in again!")
    print ("\n")
    continue

But when i execute the Program and type in for ex. "Papier" (engl. paper) it goes in the if-Statement for some reason, also for every other word i type in.

Am i missing something or what is the Problem?

Here is the whole Code:

while (1<2):
    Benutzerwahl = input("Wähle aus: Schere, Stein, Papier:")
    if Benutzerwahl != "Schere" or Benutzerwahl != "Stein" or Benutzerwahl != "Papier":
        print ("\n")
        print ("Falsche Eingabe, bitte richtig eintragen")
        print ("\n")
        continue

    print ('Du hast gewählt: ') + Benutzerwahl
    Wahloptionen = ['Schere', 'Stein', 'Papier']
    GegnerWahl = random.choice(Wahloptionen)
    print ('Ich habe gewählt: ') + GegnerWahl

    if GegnerWahl == Benutzerwahl:
        print ('Unentschieden')
    elif GegnerWahl == 'Schere' and Benutzerwahl == 'Papier':
        print('Schere schneidet Papier! Ich habe gewonnen!')
        continue
    elif GegnerWahl == 'Stein' and Benutzerwahl == 'Schere':
        print('Stein schlägt Schere! Ich habe gewonnen!')
        continue
    elif GegnerWahl == 'Papier' and Benutzerwahl == 'Stein':
        print('Papier schlägt Stein! Ich habe gewonnen')
        continue
    else:
        print('Du hast gewonnen!')
4
  • You are using the wrong logic. x != a or x != b is always true (if a and b are different). Commented Jun 2, 2022 at 7:17
  • if Benutzerwahl not in {"Schere", "Stein", "Papier"}: and while (1<2): could be a simple while True:. Commented Jun 2, 2022 at 7:18
  • Check out De Morgan's laws. Commented Jun 2, 2022 at 7:19
  • Suppose Benutzerwahl == "Schere" is True. Then Benutzerwahl != "Stein" is True. And so the whole expression is True. Commented Jun 2, 2022 at 7:25

2 Answers 2

1

Your condition is always true, because only one of the inequalities can be false at the same time. So false or true or true => true.

You should use and instead of or.

Even better, you could check whether the input is part of a set:

if Benutzerwahl not in {"Schere", "Stein", "Papier"}:
   ...
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you TimeTravelingTurtle. This also works and it looks even better than my code before. And also the explanation is quite useful. Thank you so much :)
0

use "and" instead of "or" "or" will be true as long as one of your checks is true

1 Comment

Thank you Jackboxx, that was the problem, i´t was really stupid from me. I now replaced the or with the and, and now it works perfectly. Thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.