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
does so, if(Answer1 == "text" or Answer1 == "text2"...
– Ricardo
Cool, that solved! Have some way to use parentheses not to have to repeat the variable name in all possible answers?
– Eduardo Mergener