Condition does not enter as entered

Asked

Viewed 78 times

2

No matter what I type is falling on the first if, if I type m, M or even 10 will fall in the first if. I want that if the user type f or F appears feminine and the same goes for M or m.

Letra = str(input("Qual seu gênero:"))
if Letra == "F" or "f":
    print("Feminino")
elif Letra == "M" or "m":
    print("Masculino")
else:
    ("sexo invalido")
  • the correct would be, comparison or comparison

  • When does if Letra == "F" or "f": is the same as doing if (Letra == "F") or true: and independent of the logical result of the premise (Letra == "F") anything tested with preposition or true will always return true implying that the sentence Letra == "F" or "f" always return true and the if always fall on the first case.

2 answers

4


You should make the full comparison, it doesn’t work the way you’re imagining it. The or expects as operating on each side a boolean result. One of the ways to get one is by doing a comparison operation, as you did in the first comparison by testing if the variable is equal to a given text. In the second comparison of or You’re not testing anything, you’re just putting "f", nothing is compared in fact, there is an operation that results in a boolean.

Python is a strong typing language, more or less, there are cases that she screws up there, and this is one of them. Python decided that any value except a few specific ones would be considered true if it expected a boolean value and this is what happens "f" is considered True and in a comparison of or all you have True on one side is considered True and so enters the block of if. Switching to a comparison with the equality operator (==) it really checks if it is the same and then operates correctly and gives the expected result.

I suggest studying boolean logic and understanding how the tokens of the working languages. There is no magic in the written code, it has well-defined rules of why of each thing, it doesn’t happen by chance because that’s why I always tell people to go deeper than what is seen on the surface, otherwise it will always "program" in trial and error, you’ll be able to make some things work, but you’ll never understand why, you won’t be able to create new things.

letra = input("Qual seu gênero:")
if letra == "F" or letra == "f":
    print("Feminino")
elif letra == "M" or letra == "m":
    print("Masculino")
else:
    ("sexo invalido")

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I took advantage and simplified the code.

  • Sure I understood, but pq I can not do as in my example. type if letter == "f" or "F".

  • @Rennanfelipe I was still finishing the answer, see now

3

When you simply test F without comparing to Letter without pre will be true.

if Letra == "F" or "f": # Isto está teoricamente errado.

When you simply test M without comparing with Letter without pre will be true.

elif Letra == "M" or "m": # Isto está teoricamente errado.

The correct form would be:

Letra = str(input("Qual seu gênero:"))
if Letra == "F" or Letra == "f":
    print("Feminino")
elif Letra == "M" or Letra == "m":
    print("Masculino")
else:
    ("sexo invalido")

I hope I’ve helped...

Browser other questions tagged

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