Problem using vowel lists and even numbers

Asked

Viewed 255 times

1

The exercise is as follows:

Make a program where the user type a letter and a number integer. If the letter is vowel and the number is even, or the letter is consonant and odd number, show "BAZINGA!". Otherwise, show "SHAZAM!".

I did it that way:

vogal = ["a","e","i","o","u"]
par = [0,2,4,6,8,10]
letra = input("Entre com a letra: ")
numero = input("Entre com o numero: ")
if letra in vogal and numero in par:
    print("Bazinga!")
elif letra not in vogal and numero in par:
    print("Shazam!")
else:
    print("Bazinga!")

However, when I execute this code, even if I enter with "b" and "2", the output will come out Bazinga, and as stated in the exercise should be "Shazam" the output. What’s wrong with my code? Thank you.

1 answer

1


Well, the mistake there was that you did not declare the input as whole int(), then the comparison always went wrong, because I was comparing '2'(str) with 2(int)

Another thing I changed was when checking if the number is even or odd, using % you can do this easily if numero % 2 return zero is even, if not odd.

I hope I’ve helped.

Thus remaining:

vogal = ["a","e","i","o","u"]
letra = input("Entre com a letra: ")
numero = int(input("Entre com o numero: ")) # Agora que está com int() está correto.
if (letra in vogal and numero % 2 == 0) or (letra not in vogal and numero % 2 != 0): #Uma forma melhor de checar se é ou não par.
    print("Bazinga!")
else:
    print("Shazam!")

Browser other questions tagged

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