简体   繁体   English

我想打印从 1 到 n 的数字,但在运行此代码后,出现“核心转储”错误

[英]i want to print number from 1 to n but after running this code, error "core dumped" occured

i want to print number from 1 to n but after running this code, error "core dumped" occured.我想打印从 1 到 n 的数字,但在运行此代码后,出现错误“核心转储”。 I know code works well if i change self to n but why not self ???我知道如果我将 self 更改为 n 代码运行良好,但为什么不是 self ???

n = int(input("?"))

def again_book(self):
    if n > 0:
        again_book(self - 1)
    print(self)

again_book(n)

Your code is not working because you are never changing the n variable.您的代码不起作用,因为您从未更改n变量。 In your function you're checking if n > 0 and it will always be在您的函数中,您正在检查n > 0是否始终为

Here is a fix:这是一个修复:

n = int(input("?"))

def again_book(self):
    if self > 0:
        again_book(self - 1)
    print(self)

again_book(n)

Another way of doing it is using a while loop:另一种方法是使用while循环:

n = int(input("?"))
x = 0

while x < n:
    print(x)
    x += 1

I think you can use list comprehension我认为你可以使用列表理解
[num for num in range(num_limit)] [num 范围内的 num(num_limit)]

Self is used when you have to pass object itself.当您必须传递对象本身时使用 Self。 further you can read from here你可以从这里进一步阅读

暂无
暂无

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

相关问题 运行导入 tensorflow 后出现非法指令(核心转储) - Illegal instruction (core dumped) after running import tensorflow 我想在 python 中打印一个固定字母 1080 次。我想打印 n 行而不打印到最后 - i want to print a fixed letter 1080 times in python. I want to print n number of rows without going till the end 正确编译的代码(没有错误)给出分段错误核心转储 - Rightly compiled code(without error) giving Segmentation fault core dumped OpenCv代码抛出分割错误(核心转储)Ubuntu 14.04 - OpenCv code throws segmentation error(core dumped) Ubuntu 14.04 我想在输入命令后保持我的代码运行 - I want to keep my code running after entering the command 所以,基本上我的代码在打印我希望它打印的语句后打印 None 。 我怎样才能阻止这个 None 打印 - So, basically my code is printing None after printing the statement I want it to print. How can I stop this None from printing 尝试使用 tkinter 编写“剪刀石头布”游戏。 但是我的编码没有给出我想要的,并且没有发生错误 - Trying to code 'rock paper scissors' game with tkinter. But my coding doesn't give what i want, and no error has occured 错误:分段错误(核心已转储) - Error: Segmentation fault (core dumped) 在结束程序之前的最终打印语句之后延迟。 在 c 等效项中转储的核心 - Delay after final print statement before ending the program. Core dumped in c equivalent 我想从包含 &#39;!&#39; 的列表中删除字符串字符,但在运行此代码后,我注意到它跳过了列表中的下一个字符串 - I want to remove the string from the list which contains '!' character but after running this code I noticed that it skips the next string in the list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM