1. <i id='59hOS'><tr id='59hOS'><dt id='59hOS'><q id='59hOS'><span id='59hOS'><b id='59hOS'><form id='59hOS'><ins id='59hOS'></ins><ul id='59hOS'></ul><sub id='59hOS'></sub></form><legend id='59hOS'></legend><bdo id='59hOS'><pre id='59hOS'><center id='59hOS'></center></pre></bdo></b><th id='59hOS'></th></span></q></dt></tr></i><div id='59hOS'><tfoot id='59hOS'></tfoot><dl id='59hOS'><fieldset id='59hOS'></fieldset></dl></div>
    <legend id='59hOS'><style id='59hOS'><dir id='59hOS'><q id='59hOS'></q></dir></style></legend>

    <small id='59hOS'></small><noframes id='59hOS'>

    • <bdo id='59hOS'></bdo><ul id='59hOS'></ul>

  2. <tfoot id='59hOS'></tfoot>

      For 循环仅执行 1 次,但范围为 5

      For loop only executes 1 time, though given a range of 5(For 循环仅执行 1 次,但范围为 5)
        <tbody id='LOnrT'></tbody>
      <tfoot id='LOnrT'></tfoot>

          <bdo id='LOnrT'></bdo><ul id='LOnrT'></ul>

          <legend id='LOnrT'><style id='LOnrT'><dir id='LOnrT'><q id='LOnrT'></q></dir></style></legend>
          • <small id='LOnrT'></small><noframes id='LOnrT'>

                <i id='LOnrT'><tr id='LOnrT'><dt id='LOnrT'><q id='LOnrT'><span id='LOnrT'><b id='LOnrT'><form id='LOnrT'><ins id='LOnrT'></ins><ul id='LOnrT'></ul><sub id='LOnrT'></sub></form><legend id='LOnrT'></legend><bdo id='LOnrT'><pre id='LOnrT'><center id='LOnrT'></center></pre></bdo></b><th id='LOnrT'></th></span></q></dt></tr></i><div id='LOnrT'><tfoot id='LOnrT'></tfoot><dl id='LOnrT'><fieldset id='LOnrT'></fieldset></dl></div>
                本文介绍了For 循环仅执行 1 次,但范围为 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有以下代码:

                def input_scores():
                scores = []
                y = 1
                for num in range(5):
                    score = int(input(print('Please enter your score for test %d: ' %y)))
                
                    while score < 0 or score > 100:
                        print ('Error --- all test scores must be between 0 and 100 points')
                        score = int(input('Please try again: '))
                    scores.append(score)
                    y += 1
                    return scores   
                

                当我运行它时,输出如下:

                When I run it, the output is as follows:

                Please enter your score for test 1: 
                None
                

                然后我将在无旁边输入测试分数,例如 95然后它会运行程序的其余部分,而不提示我将下一个测试分数添加到分数列表中.我真的很好奇为什么会这样

                Then I'll enter the test score next to None, as, say 95 It then runs through the rest of the program without prompting me for the next test score to add to the scores list. I'm really curious why that is

                提前感谢您抽出宝贵时间提供帮助

                Thanks in advance for taking the time to help

                真诚地,~达斯汀

                推荐答案

                你的 return 语句缩进太多,导致函数在第一次迭代时返回.它需要在 for 块之外.此代码有效:

                your return statement is indented too much, causing the function to return on the first iteration. It needs to be outside of the for block. This code works:

                def input_scores():
                    scores = []
                    y = 1
                    for num in range(5):
                        score = int(input('Please enter your score for test %d: ' %y))
                        while score < 0 or score > 100:
                            print ('Error --- all test scores must be between 0 and 100 points')
                            score = int(input('Please try again: '))
                        scores.append(score)
                        y += 1
                    return scores
                

                这篇关于For 循环仅执行 1 次,但范围为 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
              • <legend id='Fv1kK'><style id='Fv1kK'><dir id='Fv1kK'><q id='Fv1kK'></q></dir></style></legend>

                  • <bdo id='Fv1kK'></bdo><ul id='Fv1kK'></ul>
                    <tfoot id='Fv1kK'></tfoot>

                    1. <small id='Fv1kK'></small><noframes id='Fv1kK'>

                        <tbody id='Fv1kK'></tbody>

                        • <i id='Fv1kK'><tr id='Fv1kK'><dt id='Fv1kK'><q id='Fv1kK'><span id='Fv1kK'><b id='Fv1kK'><form id='Fv1kK'><ins id='Fv1kK'></ins><ul id='Fv1kK'></ul><sub id='Fv1kK'></sub></form><legend id='Fv1kK'></legend><bdo id='Fv1kK'><pre id='Fv1kK'><center id='Fv1kK'></center></pre></bdo></b><th id='Fv1kK'></th></span></q></dt></tr></i><div id='Fv1kK'><tfoot id='Fv1kK'></tfoot><dl id='Fv1kK'><fieldset id='Fv1kK'></fieldset></dl></div>