英文:
How to print a sentence based on the the first letter of the word you input (your name)?
问题
I can help you with the translation of the code part. Here is the translated code:
#温馨提示
def main():
温馨提示 = {'a':'当你脸红时,你令人难以抵挡。',
'b':'你是如何总是即使穿着运动裤也看起来很棒的呢?',
'c':'你有最棒的点子。',
'd':'每个人有时都会被击倒,但你总是能重新站起来并继续前进。',
'e':'你是周围人的礼物。',
'f':'你不仅如此,还有一大袋薯片。',
'g':'你的内在美比外表还要美丽。',
'h':'你不喜欢自己的那个地方,但那正是使你如此有趣的地方。',
'i':'你就像一股清新的空气。',
'j':'你是某人微笑的理由。',
'k':'你甚至比独角兽还要好,因为你是真实的。',
'l':'你真的很特别。',
'm':'你现在应该得到一个拥抱。',
'n':'如果有人以你为基础制作一个互联网迷因,那将是无可挑剔的语法。',
'o':'与你在一起使一切都变得更好!',
'p':'你比三球冰淇淋蛋筒还要好。带有彩糖。',
'q':'你应该更经常受到感谢。所以谢谢!',
'r':'你是给其他人的很好的榜样。',
's':'你的善良是所有遇到你的人的药膏。',
't':'当你下定决心时,没有什么能阻挡你。',
'u':'你如何珍视你的亲人令人难以置信。',
'v':'你对周围的人来说是一个很棒的朋友。',
'w':'你是周围人的礼物。',
'x':'当我情绪低落时,你总是说一些鼓励的话来帮助我感觉好些。',
'y':'当我情绪低落时,你总是说一些鼓励的话来帮助我感觉好些。',
'z':'因为你在其中,我们的社区变得更好了。'}
print('你叫什么名字?')
input_name = input('在这里输入名字:')
n1 = 'a'
n2 = 'b'
n3 = 'c'
n4 = 'd'
if input_name[0:1] == 温馨提示[n1]:
print(温馨提示['a'])
main()
This code is a Python program that gives warm compliments based on the first letter of a person's name. You can use a dictionary to store compliments associated with each letter of the alphabet, and the program will print a compliment corresponding to the first letter of the input name.
英文:
I'm trying to create some code to give 'random' warm fuzzies/compliments to people based on the first letter in their name. I'm using the dictionary function to hold the information for what compliment goes with each letter. When a (first) letter equals a letter, it will then print the compliment. I'm starting out with the letter A to get the code started and sorted. Once you input your name, it doesn't print the rest of the code.
Edit: I'm trying to match the first letter of the input name to the letter in the dictionary which then corresponds to a compliment which it will print. I'm confused as to how to do this.
#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()
This is the output it gives.:
I was thinking maybe I could input a .txt file with the info into the dictionary but I don't know how to do that.
How should I go about this? I'm a beginner with Python and thought this would be a cool starter project.
答案1
得分: 1
这可能是你想要做的事情。
letter_to_compliment_dict = {} # 代码中的大字典。
name = input("Enter your name: ")
# 我们需要名字的第一个字母,可能需要去除开头的额外空白
first_letter = name.strip()[0].casefold() # strip移除字符串开头和结尾的任何空白(默认是空白,你可以更改)。然后我们将第一个字母转换为小写,因为字典中的所有字母都是小写的。
compliment = letter_to_compliment_dict.get(first_letter, "I do not have a compliment for this letter") # 使用first_letter尝试获取一个值,如果不存在,将返回第二个参数。
print(compliment)
[0:1]的使用是不必要的,你应该使用[0]。
我假设n1、n2、n3的使用是为了添加26个if检查并相应地打印值。但你不需要使用它,这就是使用字典的好处。此外,如果你真的想要比较,可以将第一个字母与n1、n2等进行比较,但这也是使用字典的原因。
在Python中使用main
函数是不必要的。希望这有所帮助。
英文:
This is probably what you want to do.
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)
The use of [0:1] is unnecessary and you should instead use [0].
I assume that the use of n1, n2, n3 is to add 26 if-checks and print the value accordingly. But you do not need to use that and this is the advantage of using dictionary. Also, instead of comparing the first letter to the value returned by the dictionary, compare it to n1, n2 if you really want to compare. But again, that is why a dictionary is even useful.
The use of main
function in python is not necessary. Hope this helps.
答案2
得分: 0
你可以为他们的名字的第一个字母进行字典查找:
# 提示用户输入名字并将其存储在 `input_name` 中
input_name = input('在此输入名字:')
# 获取输入名字的第一个字母,并确保它是小写的,使用 `lower()` 方法
first_letter = input_name[0].lower()
# 检查输入的名字是否有效
if first_letter in warm_fuzzies:
print(warm_fuzzies[first_letter])
else:
print("你的名字不以英文字母开头!")
英文:
You can do a dictionary lookup for the first letter of their name:
# 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!")
答案3
得分: -1
#温馨的表达
def main():
温馨表达 = {'a':'你脸红的时候真是迷人。',
'b':'你怎么总是穿着运动裤都看起来那么好?',
'c':'你总是有最棒的主意。',
'd':'每个人都会跌倒,但你总是能重新站起来并继续前行。',
'e':'你是周围人的宝贝。',
'f':'你不仅仅是所有的一切,还是巨大袋装薯片。',
'g':'你内外都一样美丽。',
'h':'你不喜欢自己的那些特点是使你如此有趣的原因。',
'i':'你就像一阵清新的空气。',
'j':'你是某人微笑的理由。',
'k':'你比独角兽还要棒,因为你是真实的。',
'l':'你真的很特别。',
'm':'你现在需要一个拥抱。',
'n':'如果有人基于你制作一个网络迷因,那一定会有无可挑剔的语法。',
'o':'和你在一起,一切都变得更好!',
'p':'你比三球装冰淇淋还要好。带有彩虹糖珠。',
'q':'你应该更经常被感谢。所以谢谢你!',
'r':'你是他人的伟大榜样。',
's':'你的善良是所有遇到你的人的抚慰。',
't':'当你下定决心时,没有什么能阻挡你。',
'u':'你如何珍惜你所爱的人令人难以置信。',
'v':'你是周围人的超棒朋友。',
'w':'你是周围人的礼物。',
'x':'当我情绪低落时,你总是说些鼓励的话来帮助我感觉更好。',
'y':'当我情绪低落时,你总是说些鼓励的话来帮助我感觉更好。',
'z':'因为有你,我们的社区变得更好。'}
结果 = ""
for letter in input_name:
结果 += 温馨表达.get(letter, " ") + " "
return 结果.strip()
print('你叫什么名字?')
input_name = input('在这里输入名字:')
温馨结果 = main()
print(温馨结果)
英文:
#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.'}
result = ""
for letter in input_name:
result += warm_fuzzies.get(letter, " ") + " "
return result.strip()
print('What is your name?')
input_name = input('Insert name here: ')
fuzzy_result = main()
print(fuzzy_result)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论