如何压缩python代码?

时间:2017-07-17 15:04:05

标签: python dictionary

所以我试图让这个搜索功能在输入名字时显示人的星座。我用4个名字的手动方式做了,我知道有一种方法用我拥有的字典压缩代码(但不使用),但我不记得了。

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)

2 个答案:

答案 0 :(得分: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!")

答案 1 :(得分:0)

This can be achieved with something like so:

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

Please note this is a case sensitive check for a given name in horoscopes, i.e. if a name is input as 'angelina', it will not match to the dictionary key 'Angelina'. To account for this, if dictionary keys were known to be in lower case, one might use the string method .lower():

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

This way no matter how the name is entered, there will still be a match.

If you wanted the user to be prompted until a valid name was entered, then:

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!")