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