2014-03-03 74 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"