python 如何根据您输入的单词(您的姓名)的第一个字母打印一个句子?

pkwftd7m  于 2023-05-16  发布在  Python
关注(0)|答案(2)|浏览(185)

我正在尝试创建一些代码,根据人们名字的第一个字母给予“随机”温暖的模糊/赞美。我使用字典功能来保存每个字母的恭维信息。当一个(第一个)字母等于一个字母时,它将打印赞美。我从字母A开始,让代码开始并排序。一旦你输入了你的名字,它就不会打印剩下的代码。

**编辑:**我试图将输入名称的第一个字母与字典中的字母相匹配,然后对应于它将打印的恭维语。我不知道该怎么做。

#Warm fuzzies

def main():
    warm_fuzzies = {'a':'You\'re irresistible when you blush.',
                    'b':'How is it that you always look great, even in sweatpants?',
                    'c':'You have the best ideas.',
                    'd':'Everyone gets knocked down sometimes, but you always get back up and keep going.',
                    'e':'You are a gift to those around you.', 
                    'f':'You are all that and a super-size bag of chips.', 
                    'g':'You are even more beautiful on the inside than you are on the outside.', 
                    'h':'That thing you do not like about yourself is what makes you so interesting.',
                    'i':'You are like a breath of fresh air.', 
                    'j': 'You are someone\'s reason to smile.', 
                    'k':'You\'re even better than a unicorn, because you\'re real.',
                    'l':'You\'re really something special.', 
                    'm':'You deserve a hug right now.',
                    'n':'If someone based an Internet meme on you, it would have impeccable grammar.',
                    'o':'Being around you makes everything better!',
                    'p':'You\'re better than a triple-scoop ice cream cone. With sprinkles.', 
                    'q':'You should be thanked more often. So thank you!!',
                    'r':'You\'re a great example to others.',
                    's':'Your kindness is a balm to all who encounter it.',
                    't':'When you make up your mind about something, nothing stands in your way.', 
                    'u':'The way you treasure your loved ones is incredible.',
                    'v':'You\'re an awesome friend to those around you.', 
                    'w':'You\'re a gift to those around you.', 
                    'x':'When I\'m down you always say something encouraging to help me feel better.',
                    'y':'When I\'m down you always say something encouraging to help me feel better.', 
                    'z':'Our community is better because you\'re in it.'}
    print('What is your name?')
    input_name = input('Insert name here: ')

    n1 = 'a'
    n2 = 'b'
    n3 = 'c'
    n4 = 'd'

    if input_name[0:1] == warm_fuzzies[n1]:
        print(warm_fuzzies['a'])

main()

这是它给出的输出。:

我想也许我可以输入一个.txt文件的信息到字典,但我不知道如何做到这一点。
我该怎么做呢?我是Python的初学者,我认为这将是一个很酷的入门项目。

dced5bon

dced5bon1#

这可能是你想做的。

letter_to_compliment_dict = {}    # The huge dict that you have in the code.
name = input("Enter your name: ")
# We need the first letter of the name, we may want to remove any extra whitespace at   the beginning
first_letter = name.strip()[0].casefold()   # strip removes any whitespace at the beginning and 
# the end of the string (default is whitespace, you can change that). 
# Then we casefold (or lower) the first letter since all the letters are lowered in the dictionary

compliment = letter_to_compliment_dict.get(first_letter, "I do not have a compliment for this letter")    # Get will try to get a value using first_letter, if it does not exist, it will return the second argument.
print(compliment)

不需要使用[0:1],您应该使用[0]。我假设使用n1,n2,n3是为了增加26个if检查并相应地打印值。但是你不需要使用它,这就是使用字典的好处。另外,如果你真的想比较的话,不要把第一个字母和字典返回的值进行比较,而是把它和n1,n2进行比较。这也是为什么一本字典是有用的。在Python中使用main函数是不必要的。希望这能帮上忙。

agyaoht7

agyaoht72#

你可以在字典中查找他们名字的第一个字母:

# Prompt the user for their name and store it in `input_name`
input_name = input('Insert name here: ')

# Get the first letter of the input name and make sure it's lowercase with a call to `lower()`
first_letter = input_name[0].lower()

# Check if the name entered was valid
if first_letter in warm_fuzzies:
    print(warm_fuzzies[first_letter])
else:
    print("Your name doesn't start with a letter of the english alphabet!")

相关问题