![](/img/trans.png)
[英]How to make a function that returns all integers that are multiples of either parameters n1 or n2 within the list n
[英]How do I create a function that returns a list of integers from n to 1?
如何编写一个称为countdown
的函数,该函数从n
开始countdown
直到1? 该函数应返回一个list
,其内容应为从n
到1的整数。
def countdown(n):
if n >= 1:
countdown(n-1)
print(n)
使用递归:
def countdown(n):
if n < 1:
return []
return [n] + countdown(n-1)
一旦达到基数,此方法将提供“基本情况”并创建整数列表。
签出此链接以可视化执行。 如果您有任何疑问,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.