这是我在stackoverflow上的第一篇文章,这是我第一次使用Python编写程序。我想制作一个程序,要求用户输入学生的姓名,然后在程序继续运行时能够向该学生添加更多信息。理想情况下,我希望用户选择有多少学生,选择播放的轮数,给他们起名字,分配分数,最后输出最终分数和每个学生的平均分数。
这就是我目前的情况:
name_students = list()
num_students = input("How many students?: ")
competitionRounds = input("How many rounds: ")
score_students = list ()
myList = name_students
for i in range(1, int(num_students) + 1):
name_students.append(input("Student Name: "))
print(name_students)
for i in range (1, int(competitionRounds)):
print(input(name_students [0] + " Total Score for each round: "))
这是该程序的运行方式:
How many student?: 3 #Have the user input answers to these questions
How many rounds?: 2
Student Name: Nick
Student Name: Bob
Student Name: Lisa
Nick Total Score for each round:
我试图让它要求列出所有名称,如
Nick Total Score for round 1:
Bob Total score for round 1:
Lisa Total score for round 1:
Nick Total score for round 2:
等
我感谢任何回复的人。
--- ---编辑 所以我现在在使用用户输入的数字并通过用户放置的名称将它们加在一起时遇到问题。
我的预期结果是: 第1回合尼克总得分:2 第1回合第3轮的总得分 Lisa 1:1的总得分 尼克第2轮总得分:2 Bob第2轮的总得分:3 Lisa第2轮:1的总得分 尼克所有轮次总分:4 等
目前我的代码如下:
name_students = list()
num_students = input("How many students?: ")
competitionRounds = input("How many rounds: ")
score_students = []
myList = name_students
total_scores = 0, []
for i in range(1, int(num_students) + 1):
name_students.append(input("Student Name: "))
for i in range (1, int(competitionRounds) +1):
for j in range(len(name_students)):
score_students.append(input(name_students [j] + " Total Score for round " + str(i) +": "))
for i in range (1,int(competitionRounds) +1):
for t in range(len(score_students)):
print(name_students + score_students + total_scores)
答案 0 :(得分:1)
您需要更改最后一个循环
for i in range (1, int(competitionRounds)):
for j in range(len(name_students)):
score_students.append(input(name_students [j] + " Total Score for round " + str(i) + ": "))
这将询问用户每轮学生的每个分数,并继续将其附加到score_students
。然后你可以按照你想要的方式操纵它。
How many students?: 2
How many rounds: 3
Student Name: A
Student Name: B
['A', 'B']
A Total Score for round 1: 2
B Total Score for round 1: 1
A Total Score for round 2: 2
B Total Score for round 2: 1