Код не работает должным образом после использования while: try: loop - PullRequest
0 голосов
/ 27 января 2020

Я должен был сделать python упражнение для школы.

Это мой код:

print("Animal-determiner")
while True:
    try:
        amount =  int(input("How many animals do you want to determine? "))
    except ValueError:
        print("That was not a number.")
    else:
        break

while amount > 0:
    legs = int(input("How may legs does the animal have? "))

    if legs < 0:
        print("Such an animal doesn't exist.")
    elif legs == 1 or 3:
        print("Such an animal doesn't exist.")
    elif legs == 0:
        water = input("Does the animal live in water? ")
        if water is "yes":
            print("This is a fish")
        elif water is "no":
            print("This is a snake.")
        else:
            print("You didn't put in yes or no.")
    elif legs == 2:
        fly = input("Can the animal fly? ")
        if vliegen is "yes":
            print("This is a bird.")
        elif vliegen is "no":
            print("This has to be a bird, which can't fly.")
        else:
            print("You didn't put in yes or no.")
    elif legs == 4:
        print("This is getting to complicated, I'm stopping")

    amount -= 1

Но когда я запускаю этот код, он всегда говорит: «Такое животное не делает не существует ". даже если я добавлю: 0 или 4, которые должны вернуть что-то другое

, помощь будет принята.

Ответы [ 2 ]

0 голосов
/ 27 января 2020

legs == 1 or 3 - это не работает так. 3 будет оцениваться как условие. Поскольку он ненулевой, он оценивается как True. or не работает так, как вы думаете - это для проверки, если при хотя бы одно из двух условий True. И поскольку 3 равно True, выражение будет True. Используйте in:

legs in (1, 3)

in, чтобы проверить, является ли что-то коллекцией.

0 голосов
/ 27 января 2020

Условие elif legs == 1 or 3: неверно, оно должно быть elif legs == 1 or legs == 3: или elif legs in (1, 3):

...