Tamagochi Selection | Python 3.0 | Begginer

Viewed 82

Well, I'm trying to make an initial tamagochi selection (ignore the names, just testing), but I doesn't work.

When I type 2 or 3 or any other name than Rattatouie, I get: "Rattatouie é o seu novo Tamagochi!". In English it would be "Rattatouie is your new Tamagochi!" (I'm brazilian)

def pick_tamagochi():
    print('*'*20,"Tamagochi 2018",20*'*')
    print('1 - Rattatouie')
    print('2 - Rabbitack')
    print('3 - Sonic Hedgehog')
    TamagochiSelect = input('>>> Escolha seu Tamagochi:')

    if TamagochiSelect == '1' or 'Rattatouie':
        TamagochiSelect = 'Rattaouie'
        print('Rattatouie é o seu novo Tamagochi!')
    elif TamagochiSelect == '2' or 'Rabbitack':
        TamagochiSelect = 'Rabbitack'
        print('Rabbitack é o seu novo Tamagochi! ')
    elif TamagochiSelect == '3' or 'Sonic Hedgehog':
        TamagochiSelect = 'Sonic'
        print('Sonic é o seu novo Tamagochi!')
    else: 
        print('Nome não compatível')

print(pick_tamagochi())

Does anyone knows how I can solve it?

3 Answers

You have to use the in operator to test for multiple values:

if TamagochiSelect in ('1', 'Rattatouie'):

Otherwise TamagochiSelect == '1' or 'Rattatouie' simply means to test if TamagochiSelect is equal to '1' or if 'Rattatouie' is True, which 'Rattatouie' always is (because it is a non-empty string).

Your conditional should be written as:

if TamagochiSelect == '1' or TamagochiSelect == 'Rattatouie':

Otherwise Python will interpret it as (if TamagochiSelect == '1') or (TamagochiSelect) and non-empty strings evaluate to True (you can verify it by typing bool('some string') and bool(''))

the problem is that your boolean operator(OR) is not placed correctly, it check on the left side and sees

TamagochiSelect == '1'

this returns False but on the right it sees

'Rattatouie'

which is always True . A solution would be

if TamagochiSelect == '1' or TamagochiSelect == 'Rattatouie':
Related