遇到if / elif语句有问题

时间:2014-03-03 19:50:48

标签: python if-statement

对于代码的“dog”部分,它可以完美地工作并完成预期的工作。但是,如果您在开始时输入“Cat”作为输入问题,它仍会继续并执行代码的狗部分。

即使我在代码中写道,如果问题的答案是==“Cat”或“cat”,那么它应该执行此部分而不是Dog部分。

import time
import sys

animal=input("What animal do you want to calculate the age of? - Possible choices: Cat/Dog")

if animal=="Dog"or"dog":
    age=int(input("How old is your Dog?"))
    if age==1:
        print("Calculating the age of the Dog...")
        time.sleep(1)
        print("The age of the animal is: 11")
    elif age==2:
        print("Calculating the age of the Dog...")
        time.sleep(1)
        print("The age of the animal is: 11")
    else:
        age=age-2
        print("Calculating the age of the Dog...")
        time.sleep(1)
        agecalculation=age*4+22
        print("The age of the animal is:",agecalculation)
        time.sleep(2)
        print("End of program.")
        time.sleep(2)
        sys.exit()

elif animal=="Cat"or"cat":
    age=int(input("How old is your Cat?"))
    if age==1:
        print("Calculating the age of the Cat...")
        time.sleep(1)
        print("The age of the animal is: 15")
    elif age==2:
        print("Calculating the age of the Cat...")
        time.sleep(1)
        print("The age of the animal is: 25")
    else:
        age=age-2
        print("Calculating the age of the Cat...")
        time.sleep(1)
        agecalculation=age*4+25
        print("The age of the animal is:",agecalculation)
        time.sleep(2)
        print("End of program.")
        time.sleep(2)
        sys.exit()
else:
    print("That is not an animal, or isn't on the list specified")
    animal=input("What animal do you want to calculate the age of? - Possible choices: Cat/Dog")             

5 个答案:

答案 0 :(得分:5)

以下if测试将始终评估为true:

if animal=="Dog" or "dog":

以上工作就像它有括号一样:

if (animal=="Dog") or ("dog"):

根据Python规则,or的第二部分将始终求值为True:非空字符串的布尔值为True。

以下三个选项有效:

if animal=="Dog" or animal == "dog":

if animal in ("Dog", "dog"):

if animal.lower() =="dog":

更多:这些问题可以在python方便的交互式命令提示符上轻松测试。例如,观察"Dog"""的布尔值:

>>> bool("Dog"), bool("")
(True, False)

以下是合并声明:

>>> bool('Cat' == 'Dog' or 'dog')
True

答案 1 :(得分:0)

在python中,所有空字符串都不会被评估为true,如:

if "something":
    print("True")
else
    print("False")

将始终打印True

所以你需要设置你的if:

if animal=="Dog" or animal=="dog":
    #Do something with dog
elif animal=="Cat" or animal=="cat":
    #Do something with cat
else:
    #Do something else

答案 2 :(得分:0)

您需要编辑代码:

import time
import sys

animal=input("What animal do you want to calculate the age of? - Possible choices: Cat/Dog")

if animal=="Dog"or animal=="dog":
    age=int(input("How old is your Dog?"))
    if age==1:
        print("Calculating the age of the Dog...")
        time.sleep(1)
        print("The age of the animal is: 11")
    elif age==2:
        print("Calculating the age of the Dog...")
        time.sleep(1)
        print("The age of the animal is: 11")
    else:
        age=age-2
        print("Calculating the age of the Dog...")
        time.sleep(1)
        agecalculation=age*4+22
        print("The age of the animal is:",agecalculation)
        time.sleep(2)
        print("End of program.")
        time.sleep(2)
        sys.exit()

elif animal=="Cat"or animal == "cat":
    age=int(input("How old is your Cat?"))
    if age==1:
        print("Calculating the age of the Cat...")
        time.sleep(1)
        print("The age of the animal is: 15")
    elif age==2:
        print("Calculating the age of the Cat...")
        time.sleep(1)
        print("The age of the animal is: 25")
    else:
        age=age-2
        print("Calculating the age of the Cat...")
        time.sleep(1)
        agecalculation=age*4+25
        print("The age of the animal is:",agecalculation)
        time.sleep(2)
        print("End of program.")
        time.sleep(2)
        sys.exit()
else:
    print("That is not an animal, or isn't on the list specified")
    animal=input("What animal do you want to calculate the age of? - Possible choices: Cat/Dog")             

执行if animal == "Dog" or "dog"时,它正在评估animal == "Dog"是否"dog"。您可以使用animal.lower() == "dog",或使用上面编辑过的代码。

答案 3 :(得分:0)

pasztorpisti已经指出了明显的错误:

animal=='Cat'or'cat'不是你想要的。使用animal.lower()=='cat'animal in ['cat', 'Cat']或其他方法。

我还认为你的整个代码可以减少很多。也许您可以从以下代码段中获取一两个想法:

def dogAge(age):
    return 11 if age < 3 else age * 4 + 14

def catAge(age):
    if age == 1: return 15
    if age == 2: return 25
    return age * 4 + 17

animal = input("What animal do you want to calculate the age of? - Possible choices: Cat/Dog").lower()
if animal not in ['cat', 'dog']:
    print("That is not an animal, or isn't on the list specified")
    sys.exit(0)

age = int(input("How old is your {}?".format(animal)))

animalAge = {'cat': catAge, 'dog': dogAge}[animal](age)

print("The age of the animal is {}.".format(animalAge))

答案 4 :(得分:0)

我怀疑(没有检查过)问题不是你的逻辑,而是你的条件。

animal == "Dog" or "dog"的计算结果为(animal == "Dog") or "dog",因为“dog”是一个非空字符串,所以它总是会计算为True。请改为animal == "Dog" or animal == "dog",或者更好的是animal in ("Dog", "dog")animal.lower() == "dog"