[英]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.