简体   繁体   English

从循环返回根-Python

[英]Return root from loop - Python

I'm doing a class task and I'm stuck at returning root numbers from a loop separated by a comma. 我正在执行类任务,并且被困在用逗号分隔的循环中返回根号。 I can return one number but my loop seems to stop after that or something. 我可以返回一个数字,但是在那之后我的循环似乎停止了。 I can't really find any clues on the mighty interweb but perhaps you can. 在强大的互联网上,我真的找不到任何线索,但也许您可以找到。 The task is to return the root of i when i is between 5 and 20. So I thought of a while-loop but that might be a mistake. 我的任务是在i介于5到20之间时返回i的根。所以我想到了while循环,但这可能是一个错误。

import math

def multiplicator():
    """
    Return root from looped numbers
    """
    i = 5
    square = 0
    num = ""
    while i < 20:
        square = math.sqrt(i)
        num += str(square) + ","
        return num
        i += 1

ANSWER = multiplicator()

Could you please give me any pointers in this as I only get the root of the first number, 5. 您能否给我任何指示,因为我只得到第一个数字的根5。

Regards 问候

您需要将return语句移到while循环之外,否则一旦计算出第一个数字的根,它将立即停止该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM