Problem using lists of vowels and even numbers

1

The exercise is as follows:

  

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

I did it this 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!")

But when I run this code, even if I enter "b" and "2", the output will exit Bazinga, and as it appears in the exercise should be "Shazam" the output. What's wrong with my code? Thank you.

    
asked by anonymous 14.05.2017 / 05:25

1 answer

1

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

Another thing I've changed is when checking whether 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 have helped.

Looking like this:

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!")
    
14.05.2017 / 06:01