我需要创建一个名为f3Groups()的函数,它接受一个参数。以及一个名为cgList的列表,用于表示整个类,其中包含3个表示这3个组的列表。cgList可以是一个空列表,但是当这个班没有学生时,不能有任何空组,例如cgList = []。返回cgList,但我不知道该如何做。到目前为止,我只做了一个函数打印出用户输入列表。
def get_student_list():
stdlist = []
while True:
iStr = input('Please enter a new student name, ("quit" if no more student)')
if iStr == "quit":
return stdlist
if iStr == "":
print("Empty student name is not allowed.")
else:
stdlist.append(iStr)
stdList = get_student_list()
print("The whole class list is")
for x in stdList:
print(x, end=' ')
``
I want 7 user inputs. User input 1, 4 and 7 in one list group. User input 2 and 5 in one group. User input 3 and 6 in one group. After that print them horizontally.
``The whole class list is ['Student a', 'Student b', 'Student c', 'Student d', 'Student e'. 'Student f', 'Student g']
Group 1: ['Student a', 'Student d', 'Student g']
Group 2: ['Student b', 'Student e']
Group 3: ['Student c', 'Student f']
`
The above is the output I want. Is there a way to do it?发布于 2022-11-11 02:49:53
我想要7个用户输入。
然后你不需要一个时间真循环,你只需要迭代直到你达到7。为此,你可以使用一个while i < 8循环。并设置i = 1
用户在一个列表组中输入1、4和7。用户在一组中输入2和5。用户输入3和6为一组。
在for循环中,您可以使用i变量来检查应该将学生放在哪个组中。
在那之后的
水平地打印它们。
然后,您可以在函数返回后单独打印出来。
def f3Groups():
stdlist = [[],[],[]] # three groups so 3 lists
i = 1
while i < 8:
iStr = input('Please enter a new student name, ("quit" if no more student)')
if iStr == "quit":
return stdlist
if iStr == "":
print("Empty student name is not allowed.")
else:
if i in [1,4,7]:
stdlist[0].append(iStr)
elif i in [2,5]:
stdlist[1].append(iStr)
else:
stdlist[2].append(iStr)
i+=1 # increase i by one when student is added.
return stdlist
stdList = f3Groups()
for i, x in enumerate(stdList):
print(f"Group {i+1}: {x}")输出
Group 1: ['Student a', 'Student d', 'Student g']
Group 2: ['Student b', 'Student e']
Group 3: ['Student c', 'Student f']或者,如果你想在每个列表中一个一个地添加一个学生,那么他们都尽可能接近相同的长度。你可以这样做。
def get_student_list():
stdlist = []
while True:
iStr = input('Please enter a new student name, ("quit" if no more student)')
if iStr == "quit":
return stdlist
if iStr == "":
print("Empty student name is not allowed.")
else:
stdlist.append(iStr)
def f3Groups(stdlist):
groups = [[],[],[]]
for i,x in enumerate(stdlist):
if i % 3 == 0:
groups[0].append(x)
if i % 3 == 1:
groups[1].append(x)
else:
groups[2].append(x)
return groups
stdlist = get_student_list()
groups = f3Groups(stdlist)
for i, x in enumerate(stdlist):
print(f"Group {i+1}: {x}")创建与前面示例相同的输出。
发布于 2022-11-11 02:51:30
下面是你想要的代码:
def get_student_list():
stdlist = []
while True:
iStr = input('Please enter a new student name, ("quit" if no more student)')
if iStr == "quit":
return stdlist
if iStr == "":
print("Empty student name is not allowed.")
else:
stdlist.append(iStr)
def get_group_list(stdList):
group1 = [stdList[0], stdList[3], stdList[6]]
group2 = [stdList[1], stdList[4]]
group3 = [stdList[2], stdList[5]]
grpList = [group1, group2, group3]
return grpList
stdList = get_student_list()
grpList = get_group_list(stdList)
print("The whole class list is", stdList)
for i in range(len(grpList)):
print("Group " + str(i), grpList[i])如您所见,要水平地打印字符串后面的列表,只需添加逗号,然后在字符串后面添加变量。
a_list = [item1, item2]
print("A list: ", a_list)指纹:
A list: [item1, item2]此外,为了让学生分组,使用嵌套列表作为简单的解决方案。这涉及到列表中的列表。学生可以分配指数。
https://stackoverflow.com/questions/74397418
复制相似问题