1. <tfoot id='6WHHT'></tfoot>
      <legend id='6WHHT'><style id='6WHHT'><dir id='6WHHT'><q id='6WHHT'></q></dir></style></legend>

      <small id='6WHHT'></small><noframes id='6WHHT'>

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

    2. Python For循环多次返回

      时间:2023-09-02

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

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

            <tbody id='HFvUN'></tbody>
            <bdo id='HFvUN'></bdo><ul id='HFvUN'></ul>
            <tfoot id='HFvUN'></tfoot>

                本文介绍了Python For循环多次返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我应该用 python 为我的一门计算机科学课程编写一个函数.该函数应该接受一个 startValue 然后递增它直到达到 numberOfValues .到目前为止,这是我的功能:

                I am supposed to write a function for one of my computer science classes in python. The function is supposed to take in a startValue and then increment it until numberOfValues is reached. This is my function so far:

                def nextNValues(startValue, increment, numberOfValues):
                   result = int(0)
                   for i in range(0, numberOfValues):
                      increase = i * increment
                       result = startValue + increase
                   return result
                

                我这样称呼它:

                print(nextNValues(5,4,3))
                

                问题是输出只有 13.我怎样才能让它在每次增加时返回一个数字.例如,5、9、13?我以前的函数一直有这个问题,但我只是在没有太多逻辑的情况下添加和删除东西来让它工作.我做错了什么?

                The problem is that the output is only 13. How do I make it so it returns a number each time it increments. For example, 5, 9, 13? I have been having this problem with my previous functions but I have just been adding and removing things without much logic to get it to work. What am I doing wrong?

                推荐答案

                这是 发电机.

                长话短说,只需使用 yield 而不是 return:

                Long story short, just use yield instead of return:

                def nextNValues(startValue, increment, numberOfValues):
                  result = int(0)
                  for i in range(0, numberOfValues):
                    increase = i * increment
                    result = startValue + increase
                    yield result
                

                您的代码的客户端可以在一个简单的循环中使用它:

                The clients of your code can then use it either in a simple loop:

                for value in nextNValues(...):
                  print(value)
                

                如果需要,他们可以通过 list 转换得到一个列表.例如,如果需要打印结果:

                Or they can get a list if needed by converting it with list. For example, if one needed to print the result:

                print(list(nextNValues(...)))
                

                这篇关于Python For循环多次返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:递归返回链表的中间节点 下一篇:Python3 exec,为什么返回None?

                相关文章

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

                    <small id='7ap3s'></small><noframes id='7ap3s'>