基本上是标题。由于某些原因,此代码会在每次输入后打印出首字母缩写,这不是我想要的。
num_of_people = int(input("How many people will attend the meeting? "))
for i in range(num_of_people):
name = input("What is the full name of participant " + str(i+1) + "?")
split_name = name.split(" ")
for i in range(len(split_name)):
name_word = split_name[i]
print (name_word[0])
我希望它列出参加会议的所有人员的姓名缩写。
答案 0 :(得分:1)
它每次打印一次,因为您将它放在一个循环中。对于任何程序,您都希望将其分解为一组需求,然后建立如何满足这些需求的方法。根据您的问题,我认为要求是:
您完全符合条件1。不满足要求2。您接受每个名称并将其存储为一个循环,但是一旦到达下一个迭代,该名称就会被覆盖。您应该有一个包含每个名称的列表。对于要求3,如果要在输入所有名称后显示首字母缩写,则需要最后写该循环。我将尝试使用超描述性变量。
num_of_people = int(input("How many people will attend the meeting? "))
name_list = [] # This will be the list we store each name in.
for i in range(num_of_people):
name = input(f"What is the full name of participant {i+1}? ")
# We can use literal f-strings to format strings easier.
name_list.append(name) # We add the name to our name list
# This next line is so you can see what's happening. Take it out after.
print(f"Name list is currently: {name_list}")
# We're done building the name list now. We want to print the initials
print("---Initials---")
for cur_name in name_list: # cur_name is the current name
split_name = cur_name.split(" ")
# split_name is now a list of each 'word' from the current name
# If we want to take the initials only, we need to grab the first
# element of each word in this list.
for sub_name in split_name:
# Now ask yourself how you want it to be formatted?
# A common way would be with dots between each initial
initial = sub_name[0]
print(f"{initial}.", end = "")
# We add end = "" so that no new-line is printed
# After we print all the initials for the current name, add a newline.
print()
输出:
How many people will attend the meeting? 4
What is the full name of participant 1? Pam Beezly Halpert
Name list is currently: ['Pam Beezly Halpert']
What is the full name of participant 2? Michael Scott
Name list is currently: ['Pam Beezly Halpert', 'Michael Scott']
What is the full name of participant 3? Dwight Schrute
Name list is currently: ['Pam Beezly Halpert', 'Michael Scott', 'Dwight Schrute']
What is the full name of participant 4? Holly Flax
Name list is currently: ['Pam Beezly Halpert', 'Michael Scott', 'Dwight Schrute', 'Holly Flax']
---Initials---
P.B.H.
M.S.
D.S.
H.F.
我建议您花一些时间在线查看教程。官方网站上有一些很棒的游戏:https://docs.python.org/3/tutorial/
答案 1 :(得分:1)
这是可以完成此操作的另一种方法,仅使用两个循环并使用以下技术:
join()
字符串函数这些是非常有用的工具,可以放在口袋里。 :-)
# User prompt
num_of_people = int(input("How many people will attend the meeting? "))
# Initialise
initials = []
names = []
names_split = []
# Prompt user for each name. Use proper case for each name, regardless of input.
for i in range(1, num_of_people+1):
full_name = input("What is the full name of participant {}? ".format(i)).title()
names.append(full_name)
names_split.append(full_name.split())
# Loop through names and build list of upper case initials.
for name in names_split:
initials.append(''.join([i[0].upper() for i in name]))
# Print results.
print('\nNumber of participants:', num_of_people)
print('\nParticipant names:\n', names)
print('\nParticipant initials:\n', initials)
输出:
How many people will attend the meeting? 6
What is the full name of participant 1? One two THREE Four
What is the full name of participant 2? Sheldon Cooper
What is the full name of participant 3? Leonard hofstadter
What is the full name of participant 4? penny
What is the full name of participant 5? HOWARD woloWITZ
What is the full name of participant 6? raj koothrappali
Number of participants: 6
Participant names:
['One Two Three Four', 'Sheldon Cooper', 'Leonard Hofstadter', 'Penny', 'Howard Wolowitz', 'Raj Koothrappali']
Participant initials:
['OTTF', 'SC', 'LH', 'P', 'HW', 'RK']
希望这会有所帮助!
答案 2 :(得分:-1)
所以我们可以尝试这样的事情:
num_of_people = int(input("How many people will attend the meeting? "))
for i in range(num_of_people):
name = input("What is the full name of participant " + str(i+1) + "?")
## This loop stuff is not needed...
## split_name = name.split(" ")
## for i in range(len(split_name)):
## name_word = split_name[i]
print (name[0])
代码中的错误已在注释中指出,因此请多多关照:)