简体   繁体   English

我想在单独的行上打印每个数字的平方

[英]I want print the square of each number on a separate line

def square_non (n):
    for x in range(0,21):
         if x < n:
             return (x*x)
         else:
            return "Please try again!"

Tried to debug but I don't comprehend why the for-loop isn't continuing.试图调试,但我不明白为什么 for 循环没有继续。

Update: Just used list-comprehension and generators, which made the whole thing easy更新:刚刚使用了列表理解和生成器,这让整个事情变得简单

If you want to print the values, you should not return them but print them.如果要打印这些值,则不应返回它们而应打印它们。

def square_non (n):
    for x in range(0,21):
         if x < n:
             print(x*x)
         else:
             print("Please try again!")

If you return x*x , this function will terminate at the moment it reaches this return statement, which is in the first run of the loop.如果您返回x*x ,则此 function 将在到达此返回语句时终止,该语句位于循环的第一次运行中。 So, the function just returns 0, no matter what you pass as an argument.因此,无论您作为参数传递什么,function 都只会返回 0。

Return will end the execution of loop and will return control to calling function. Return 将结束循环的执行,并将控制权返回给调用 function。 So best way is to get all values in array and return it.所以最好的方法是获取数组中的所有值并返回它。

Try this code:试试这个代码:

def square_non (n):
    squares = [] 
    for x in range(0,21):
         if x < n:
             squares.append(x*x)
         else:
             return squares

def calling_function():   #Function from which it get called
    squares = square_non(10) #Assume 10 for example 
    for square in squares:
        print(str(square) + '\n')

If you want to print the value in seperate line then simple use print(square_non(n),'\n') and add '\n' which will create new line.如果你想在单独的行中打印值,那么简单地使用print(square_non(n),'\n')并添加'\n'这将创建新行。

Here is the code这是代码

def square_non (n):
    for x in range(0,21):
         if x < n:
             return (x*x)
         else:
            return "Please try again!"
print(square_non(n),'\n')

As they mentioned in the comments already the issue here is the return because a loop as soon as hits a return it will terminate it.正如他们在评论中已经提到的那样,这里的问题是返回,因为一旦到达返回,循环就会终止它。

The most simple fix for your code would be this --->:对您的代码最简单的修复是这样的--->:

def square_non(n):

for x in range(0, 21):
    
    if x < n:
        print(x*x)
    else:
        print("Please try again!")
        return

        
x = square_non(5)

As you can see I do use return after the 'else' statement exactly for the reason why your code was wrong, to terminate the code.正如您所看到的,我确实在“else”语句之后使用 return 来终止代码,这正是您的代码错误的原因。

As also was mentioned by a comment earlier, you can append the result into a list:正如之前评论中提到的,您可以将 append 的结果放入列表中:

def square_non(n):

squared_list = []

for x in range(0, 21):
    
    if x < n:
        x = (x * x)
        squared_list.append(x)
    else:
        print("Please try again!")
        break
return squared_list
    

        
x = square_non(10)
print(x)

So the list is stored now in X, and you can use it.所以列表现在存储在 X 中,您可以使用它。 I mentioned this because it's a great way to understand what return does:我提到这一点是因为这是了解 return 作用的好方法:

  1. Terminate the execution of a Loop终止循环的执行
  2. Return a value, data..basically something that then you can store it in a variable (if you don't do this, the function would return None)返回一个值,data..basically 一些东西,然后你可以将它存储在一个变量中(如果你不这样做,function 将返回 None)

暂无
暂无

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

相关问题 Python - For 循环,在单独的行上打印每个数字及其平方 - Python - For Loop That Prints Each Number And Its Square on a Separate Line 我想在不同的行上打印每一行 - I want to print each line on a different row 我已经使用列表作为每个键的值制作了一个字典,我想打印没有方括号的值 - I have made a dictionary using a list as the values for each key, I want to print the values without square brackets 如何打印文件中每行的字数? - How to print the number of words in each line in a file? 我想使用python提取并打印每个块的第一行中包含10917、11396和1116920的块 - Using python, I want to extract and print blocks that contain 10917, 11396 and 1116920 in first line of each block 我想在给定列数的情况下以矩阵形式打印每个单词中的字母 - I want to print the letters in each word in matrix form given the number of columns 我想在我的文本文件中打印以特定许可证号开头的行,但这仅在许可证号在第一行时才会打印出来 - I want to print the line in my textfile that starts with a specific license number, but this only prints out if the license number is in the 1st line 我想获取用户提交的字符串,并在日志文件中搜索确切的字符串,然后打印行号 - I want to take a user submitted string and search for that exact string in a log file and print the line number 我想在比赛前后打印该行 - I want to print the line before and after match Python:我有一个单词列表,想检查这些单词在文件的每一行中出现的次数 - Python: I have a list of words, and want to check the number of occurrences of those words in each line in a file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM