def from10to1():
for a in range(10, 0, -1):
print(a)
# call with
from10to1()
如果要使用参数,可以执行以下操作:
def countdown(start,stop):
for a in range(start, stop, -1):
print(a)
countdown(10,1)
如果要返回应打印的字符串:
def from10to1():
# note I'm using "\n" this puts each number on the same line,
# use " " to separate them with spaces instead of newlines.
return "\n".join(str(i) for i in range(10, 0, -1))
print(from10to1())
看起来你想要一个for循环,范围产生一个大于10到1的整数。itter中的每个数字都将放入
a
,然后执行for
之后的代码。你知道吗如果要使用参数,可以执行以下操作:
如果要返回应打印的字符串:
相关问题 更多 >
编程相关推荐