简体   繁体   中英

Code doesn't work properly after using a while: try: loop

I had to do a python exercise for school.

This is my code:

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

But when i run this code it always says: "Such an animal doesn't exist." even if i put in a: 0 or a 4 which should give back something different

help would be appreciated.

条件elif legs == 1 or 3:是错误的,应该是elif legs == 1 or legs == 3: or elif legs in (1, 3):

legs == 1 or 3 - that doesn't work like that. 3 will be evaluated as a condition. Because it is nonzero, it evaluates as True . or does not work like you may think - it is for checking if at least one of two conditions are True . And because 3 is True , the expression will be True . Use in :

legs in (1, 3)

in checks if something is a collection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM