如何处理python任务

2024-04-25 19:26:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python的新手,我想做几个任务。你知道吗

有人能帮我怎么做吗?你知道吗

实际上我得打印10到0之间的数字。我可以通过这个循环来解决这个问题。你知道吗

a = " ".join(str(i) for i in range(10, 0, -1))
print (a)

但我需要使用如下函数。当我在NetBeans中执行它时,我没有得到任何结果。你知道吗

def from10to1( ) :
a=range(10, 0, -1)
print(a)

Tags: 函数infordefrange数字printjoin
1条回答
网友
1楼 · 发布于 2024-04-25 19:26:15

看起来你想要一个for循环,范围产生一个大于10到1的整数。itter中的每个数字都将放入a,然后执行for之后的代码。你知道吗

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())

相关问题 更多 >