Problem with the conditional IF structure

Asked

Viewed 98 times

2

I’m trying to learn Python (and programming in general). At the moment, I’m studying if and Else and conditions, or and not. I’m training doing a quiz on star Wars, but I’m having a hard time.

Score = 0

Resposta1 = input("No filme Star Wars, Episódio IV, Léia manda uma mensagem de socorro que diz: \n \" Help me ___________, "
         "you're my only hope.\" Para quem Léia estava pedindo ajuda?")

if Resposta1 == "Obi Wan Kenobi" or "Obi-Wan Kenobi" or "Obi Wan" or "obi wan":
    print("Boa, você é mesmo um Jedi!")
    Score = Score + 1
else:
  print("Você tem muito a aprender, jovem Padawn.")

print("Seu score atual é: " + str(Score))

The program knew to distinguish between a right and a wrong answer before it added other spellings of Obi Wan with the "and". Now, even wrong answers return a positive answer. Where I am missing?

  • does so, if(Answer1 == "text" or Answer1 == "text2"...

  • Cool, that solved! Have some way to use parentheses not to have to repeat the variable name in all possible answers?

1 answer

6

How Python Works

In the Python one string empty in a condition the return is false, however a string with some value is returning true, see an example:

Example 1

if "uma string qualquer" :
  print("Tudo ok")
else:
  print("Não deu")

Example 2

Upshot: All right

if "" :
  print("Tudo ok")
else:
  print("Não deu")

Upshot: We didn’t get

The problem

In his case, he returned false in the first condition Resposta1 == "Obi Wan Kenobi", but the other strings were returning true, since they were not empty.

You need to compare the variable with all strings

if Resposta1 == "Obi Wan Kenobi" or Resposta1 == "Obi-Wan Kenobi" or Resposta1 == "Obi Wan" or Resposta1 == "obi wan":

That’s why you were always returning a positive value, see your edited code:

Score = 0

Resposta1 = input("No filme Star Wars, Episódio IV, Léia manda uma mensagem de socorro que diz: \n \" Help me ___________, "
         "you're my only hope.\" Para quem Léia estava pedindo ajuda?")

if Resposta1 == "Obi Wan Kenobi" or Resposta1 == "Obi-Wan Kenobi" or Resposta1 == "Obi Wan" or Resposta1 == "obi wan":
    print("Boa, você é mesmo um Jedi!")
    Score = Score + 1
else:
  print("Você tem muito a aprender, jovem Padawn.")

print("Seu score atual é: " + str(Score))

Code with improvements

To make it easier you can use the "in", example:

if Resposta1 in ["Obi-Wan Kenobi", "Obi Wan", "obi wan"]:

The same example using your code:

Score = 0

Resposta1 = input("""
No filme Star Wars, Episódio IV, Léia manda uma mensagem de socorro que diz:
Help me ___________, you're my only hope.\" Para quem Léia estava pedindo ajuda?
""")

if Resposta1 in ["Obi-Wan Kenobi", "Obi Wan", "obi wan"]:
    print("Boa, você é mesmo um Jedi!")
    Score = Score + 1
else:
  print("Você tem muito a aprender, jovem Padawn.")

print("Seu score atual é: " + str(Score))

This way it checks whether the answer is in the array, if yes, he returns true, I took the tip of the fernandosavio and used multiline strings for easier reading.

Test here

  • Cool, that solved! Have some way to use parentheses not to have to repeat the variable name in all possible answers?

  • I’ll make a change and put it right away

  • You can use "in", I edited my answer using

  • 1

    You can take advantage and teach how to use multiline strings (""") not have to add the \n and the code becomes more readable. ;)

  • Ah, so it would be a list of possible answers and the code checks if one of them was used. Beauty, thank you!

  • For nothing! Do not forget to evaluate positively the question and mark as the answer (y)

  • @fernandosavio, thanks for the tip, I’ve edited!

  • 1

    Now it’s beautiful! : D My +1 had already won hahah

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.