Problem where IF and ELIF do not work in Python

Asked

Viewed 211 times

5

First time programming, I started a week ago and I gave up trying something. However, I have a problem where, no matter what I put in the variable, it always shows the same result, which in the case is :"Streamshop, R$89.99".
I have tried to define the type from variable to str, rewrite all the code, decrease the amount of characters, nothing worked. I’m using Google’s Colab to program.

x = input("Diga-me a estação que deseja fazer a compra: \n")

if x.lower() == "verão" or "verao":
  print("StreamShop,","R$89,99")

elif x.lower() == "primavera":
  print("Loajing,", "R$84,00")

elif x.lower() == "outono":
  print("Showpping,", "R$73,00")

elif x.lower() == "inverno":
  print("Loajing,", "R$139,00")

else:
  print("Valor inválido")

3 answers

7


Problem:

The flow of your code will always fall into print("StreamShop,","R$89,99"). That’s because on the line...

if x.lower() == "verão" or "verao":

...is being instructed to the program to carry out the following sequence of operations:

  • x.lower() == "verão" an operation to compare the equality between two values whose result can return True or False depending on the value of x.lower() be equal or not to string verão. Which implies two possible expansions for expressions:

  • True or bool("verao") in the case of x.lower() == "verão" result in True.

  • False or bool("verao") in the case of x.lower() == "verão" result in False.

But as seen in that reply the logical operator or returns True one of its premises being true, then in True or bool("verao") the interpreter automatically proceeds by expanding the expression to only True thus ignoring the expression part of the expression or bool("verao") because its result is irrelevant.
Already in False or bool("verao") the interpreter converts bool("verao") in True using the truth test procedure, thus ignoring the expression part of the expression False or because its result is irrelevant.

So no matter the value of x.lower() for x.lower() == "verão" or "verao" will always be True and the flow of your code will always fall into print("StreamShop,","R$89,99").

Solution 1:

An immediate repair to your code would be in the first comparison to test if x.lower() is contained in the ensemble of words {"verão", "verao"} using the operator in:

x = input("Diga-me a estação que deseja fazer a compra: \n").lower()

if x in {"verão", "verao"}:
  print("StreamShop,","R$89,99")    
elif x == "primavera":
  print("Loajing,", "R$84,00")    
elif x == "outono":
  print("Showpping,", "R$73,00")    
elif x == "inverno":
  print("Loajing,", "R$139,00")    
else:
  print("Valor inválido")

Test the example on Repl.it

Solution 2:

Older people usually write primavery, summertime, autumn and airy, then another possibility of repair would be to remove all the accentuation of the input using the method unicodedata.normalize() to obtain the union equivalent input using the form of compatibility and composition normalization (NFKD) which basically modifies the representation of accented Unicode characters in their conjugated equivalent without changing the visual representation of the character, be it the word summertime, would continue to be displayed as summertime but the character ã is decomposed in the conjugate to + ~. Then a conversion of this normalized string with the method is made str.() in ASCII to remove the conjugate, in case the accents, and finalmete with the method bytes.decode() and returned to UTF8 string without accentuation and special characters:

import unicodedata

x = input("Diga-me a estação que deseja fazer a compra: \n").lower()

x = unicodedata.normalize('NFKD', x).encode('ascii', 'ignore').decode('utf8')

if x == "verao":
  print("StreamShop,","R$89,99")    
elif x == "primavera":
  print("Loajing,", "R$84,00")    
elif x == "outono":
  print("Showpping,", "R$73,00")    
elif x == "inverno":
  print("Loajing,", "R$139,00")    
else:
  print("Valor inválido")

Test the example on Rep.it

5

The problem with your program is in the second if condition. I will divide this if into 2 for you to notice:

Original:

if x.lower() == "verão" or "verao":

Divided:

if x.lower() == "verão":

if "verao":

Note that in the first if the word summer is compared to the variable x, but in the second if this does not occur and this generates the logical error. For the second if it works as expected we would need to leave:

if x.lower() == "verao":

Then the if would be so:

if x.lower() == "verão" or x.lower() == "verao":

The reason the first if is activated is that by default anything other than zero is considered true. You can test the following if and see that only the zero that will not be activated:

if 0: print('Não ativou')

if 1: print('Ativou')

if "Isso é diferente de zero": print('Ativou')

if 'Isso também': print('Ativou')

if 'verao': print('Ativou')

2

I noticed that you could optimize your code by putting the treatment functions of string following them, therefore, if you can verify, I gave myself the freedom to add a simple function that unlocked your problem.

The purpose of this function .replace(), has as main objective to exchange a certain part of your string to another that you prefer, thus optimizing the assertive response of the end user.


x = input("Diga-me a estação que deseja fazer a compra - evite acentuação, "
          "por gentileza: \n").lower().replace('verão', 'verao')

if x == "verao":
    print("StreamShop,", "R$89,99")

elif x == "primavera":
    print("Loajing,", "R$84,00")

elif x == "outono":
    print("Showpping,", "R$73,00")

elif x == "inverno":
    print("Loajing,", "R$139,00")

else:
    print("Valor inválido")

Browser other questions tagged

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