2014-03-03 73 views
0

对于代码中的“狗”部分,它完美地工作并完成它应有的功能。但是,如果在开始时为输入问题输入“Cat”,它仍会继续并执行代码的狗部分。使用if/elif语句遇到问题

即使我在代码中写道,如果问题==“猫”或“猫”的答案,那么它应该做这个部分而不是狗部分。

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")    
+1

请使用一致的缩进(每个级别总是4个空格)。这在Python中具有语义相关性。 – Nabla

+2

'如果动物==“狗”或动物==“狗”:'但也许你应该写'如果animal.lower()==“狗”:' – pasztorpisti

+0

感谢您的快速回复pasztorpisti - 第一部分解决了它完美。荣誉给你。 – user3376281

回答

5

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

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

向上述作品,好像有括号:

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

or的第二部分将,下Python的规则,总是为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 
0

在Python中,根本没有空字符串被评价为true,如:

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

将始终打印True

所以你需要设置你是否喜欢:

if animal=="Dog" or animal=="dog": 
    #Do something with dog 
elif animal=="Cat" or animal=="cat": 
    #Do something with cat 
else: 
    #Do something else 
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",或使用上面编辑的代码。

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)) 
0

我怀疑(不包括在检查的话),这个问题是不是你的逻辑,你的条件。

animal == "Dog" or "dog"的计算结果为(animal == "Dog") or "dog"由于“dog”是一个非空字符串,它将始终计算为True。试试animal == "Dog" or animal == "dog",还是更好些animal in ("Dog", "dog")animal.lower() == "dog"