我正在尝试创建一些代码,根据人们名字的第一个字母给予“随机”温暖的模糊/赞美。我使用字典功能来保存每个字母的恭维信息。当一个(第一个)字母等于一个字母时,它将打印赞美。我从字母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的初学者,我认为这将是一个很酷的入门项目。
2条答案
按热度按时间dced5bon1#
这可能是你想做的。
不需要使用[0:1],您应该使用[0]。我假设使用n1,n2,n3是为了增加26个if检查并相应地打印值。但是你不需要使用它,这就是使用字典的好处。另外,如果你真的想比较的话,不要把第一个字母和字典返回的值进行比较,而是把它和n1,n2进行比较。这也是为什么一本字典是有用的。在Python中使用
main
函数是不必要的。希望这能帮上忙。agyaoht72#
你可以在字典中查找他们名字的第一个字母: