【问题标题】:Flow control in a function函数中的流控制
【发布时间】: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


【解决方案1】:

您需要更改 while 和 for 循环的顺序。这是您的代码的有效重构:

 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 
        for student in pretest_scores:
            print student
            while current_question <= question_number:
                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 
            current_question = 1                                                                                                                                                                                       

        print pretest_scores

    scores_on_pretest()

【讨论】:

    【解决方案2】:

    只需颠倒两个循环的顺序即可。因此,您首先迭代学生,然后为每个学生迭代每个问题。

    print("First we will collect the student names on the pre-test.")
    
    def student_names_pretest():
        students_pretest = []
        while True:
            name = raw_input("Type in a name or type 'done':")
            if name == "done":
                break
            students_pretest.append(name)
    
        students_pretest.sort()
        return students_pretest
    
    def scores_on_pretest(num_questions): 
        pretest_scores = {}
    
        for name in students_pretest:
            pretest_scores[name] = []
    
        for student in pretest_scores:
            print ("Pre-test scores for %s" % student)
            for current_question in range(num_questions):
                score = int(raw_input("Enter a score for Question #%d: " % (current_question + 1)))
                pretest_scores[student].append(score)
    
        return pretest_scores
    
    if __name__ == '__main__':
        students_pretest = student_names_pretest() 
        print(students_pretest)
    
        num_questions = int(raw_input("How many questions are on this assessment?: "))
        pretest_scores = scores_on_pretest(num_questions)
        print(pretest_scores)
    

    请注意,遍历 pretest_scores dict,您不会按照输入的顺序让学生返回,如果这很重要?如果是这样,您可以使用来自collections 模块的OrderedDict

    【讨论】:

    • 有道理 - 我知道这与订单有关。我对您输入的代码有一个疑问。我了解您更改功能的某些内容并清理内容所做的工作。你能向我解释一下以 if name == 'main" 开头的块吗?这是在引用一个类吗?
    • 没有问题。这不是绝对必要的。它可以被拿走,它不会停止它的工作。它所做的只是允许将脚本用作模块或自包含脚本,请参阅here 以获取完整说明。看到这个脚本作为一个模块可能具有有限的重用价值,它实际上并没有给脚本增加太多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多