所以我試圖讓這個搜索功能顯示名字輸入時人的星座。我用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)
你能澄清你的問題,你的目標是什麼,你的問題是不完全清楚在什麼條件你正在尋找成果。 –
嗨,我正在尋找將整個功能塊壓縮成只有一個「如果」和「其他」。因此,如果名稱出現在詞典中,那麼if可能會是這樣的,那麼該名稱將與星座運算的值一起打印 –
請參閱@RacezeQ做了什麼。壓縮的最佳方法是在字典中查找名稱並返回與該名稱關聯的值。 –