2017-07-17 101 views
-1

所以我試圖讓這個搜索功能顯示名字輸入時人的星座。我用4個名字做了手動方式,我知道有一種方法可以用我已經(但不使用)的字典壓縮代碼,但我不記得了。如何壓縮python代碼?


Horoscopes = { 
    "A": "Scorpio", 
    "B": "Gemini", 
    "J": "Sagittarius", 
    "P": "Gemini", 
    } 

def horoscope(name): 
    if name == "A" or name == "a": 
     print ("Hello " + name + ", you are a Scorpio!") 
     print("Welcome to the Horoscope Search!") 
     name = input("What is your name? ") 
     horoscope(name) 
    elif name == "B" or name == "b": 
     print ("Hello " + name + ", you are a Gemini!") 
     print("Welcome to the Horoscope Search!") 
     name = input("What is your name? ") 
     horoscope(name) 
    elif name == "J" or name == "j": 
     print ("Hello " + name + ", you are a Sagittarius!") 
     print("Welcome to the Horoscope Search!") 
     name = input("What is your name? ") 
     horoscope(name) 
    elif name == "P" or name == "p": 
     print ("Hello " + name + ", you are a Gemini!") 
     print("Welcome to the Horoscope Search!") 
     name = input("What is your name? ") 
     horoscope(name) 
    else: 
     print ("Sorry " + name + ", you are not registered in our 
     system!") 
     print("Welcome to the Horoscope Search!") 
     name = input("What is your name? ") 
     horoscope(name) 

print("Welcome to the Horoscope Search!") 
name = input("What is your name? ") 
horoscope(name) 
+1

你能澄清你的問題,你的目標是什麼,你的問題是不完全清楚在什麼條件你正在尋找成果。 –

+0

嗨,我正在尋找將整個功能塊壓縮成只有一個「如果」和「其他」。因此,如果名稱出現在詞典中,那麼if可能會是這樣的,那麼該名稱將與星座運算的值一起打印 –

+1

請參閱@RacezeQ做了什麼。壓縮的最佳方法是在字典中查找名稱並返回與該名稱關聯的值。 –

回答

1

你應該定義你的字典用鑰匙從小寫字母開始,這樣你就可以分析所有的答案,以降低字母和這樣比較吧:

Horoscopes = { 
    "a": "Scorpio", 
    "b": "Gemini", 
    "j": "Sagittarius", 
    "p": "Gemini", 
} 

def horoscope(name): 
    if name.lower() in Horoscopes: 
     print("Hello " + name + " you are a " + Horoscopes[name.lower()] + "!") 
    else: 
     print("Sorry " + name + ", you are not registered in our system!") 
+0

如何編寫「如果」,以便如果名稱未出現在字典中,則代碼將運行其他 –

+0

如果在星座中沒有name.lower(): print(「Sorry」+ name +「,you 「) else: print(」Hello「+ name +」you is a「+ Horoscopes [name.lower()] +」!「) – RaczeQ

0

這東西可實現像這樣:

horoscopes = { 
    "Angelina": "Scorpio", 
    "Bernice": "Gemini", 
    "Jessica": "Sagittarius", 
    "Peniel": "Gemini", 
    } 

print("Welcome to the Horoscope Search!") 
name = input("What is your name? ") 

if name in horoscopes: # If name is a key in horoscopes dict 
    print("Your Horoscope is {}!".format(horoscopes[name])) 

請注意這是一個大小寫敏感的檢查星座中給定的名字,即我如果某個名字被輸入爲'angelina',它將不會與字典鍵'Angelina'相匹配。考慮到這一點,如果字典鍵被稱爲是在情況下,人們可能會使用字符串方法.lower()

name = input("What is your name? ").lower() 

這樣,無論怎麼輸入名稱,仍然會有匹配。

如果你希望用戶被提示,直到一個有效的名字被輸入,則:

horoscopes = { 
    "angelina": "Scorpio", 
    "bernice": "Gemini", 
    "jessica": "Sagittarius", 
    "peniel": "Gemini", 
    } 

print("Welcome to the Horoscope Search!") 

while True: # Until a name is matched to horoscopes 
    name = input("What is your name? ").lower() 

    if name in horoscopes: # If name is a key in horoscopes dict 
     print("Your Horoscope is {}!".format(horoscopes[name])) 
     break # A valid name has been entered, break from loop 
    else: 
     print("Please enter a valid name!")