【发布时间】:2015-09-29 21:39:50
【问题描述】:
scores_on_pretest 函数有问题。
如果我输入两个名字,比如说 Joe 和 Bob,当脚本到达这个函数时,它会询问 Joe 的问题 #1 的分数,然后是 Bob 的问题 #1 的分数,然后是 Joe 的问题 #2 的分数,等等。
我想做的就是先把所有问题都问给乔,然后再问鲍勃。我知道它一定是在错误的地方表达,但似乎无法找到哪个。
感谢您的任何建议!
students_pretest = []
print "First we will collect the student names on the pre-test."
def student_names_pretest():
while True:
name = raw_input("Type in a name or type 'done':")
if name == "done":
break
students_pretest.append(name)
students_pretest.sort()
print students_pretest
student_names_pretest()
pretest_scores = {}
for name in students_pretest:
pretest_scores['%s' % name] = []
question_number = int(raw_input("How many questions are on this assessment?: "))
def scores_on_pretest():
current_question = 1
while current_question <= question_number:
for student in pretest_scores:
print "Pre-test scores for %s" % student
score = int(raw_input("Enter a score for Question #%d: " % current_question))
pretest_scores[student].append(score)
current_question =+ current_question + 1
print pretest_scores
scores_on_pretest()
【问题讨论】:
-
请在您的问题中添加“python”标签。
-
您是否尝试过颠倒循环的顺序?所以你在外循环中迭代学生,然后在内循环中迭代问题?另请注意,当您将学生存储在 dict 中时,它不会保留您输入学生姓名的顺序。
标签: python function while-loop controls