繁体   English   中英

Python 循环 N 到 0 或 -N 到 0

[英]Python Loops N to 0 or -N to 0

We will pass in a value N. N can be positive or negative.

If N is positive then output all values from N down to and excluding 0.
If N is negative, then output every value from N up to and excluding 0.

我被这个挑战困住了,我不知道如何编码或者只输出正值或负值。

有什么帮助吗?

你能检查一下这段代码,让我知道这是否有效。

def printNumber(num):
    #If N is positive then output all values from N down to and excluding 0.
    if num > 0:
        for i in range(num, 0, -1):
            print(i)
    #If N is negative then output all values from N up to and excluding 0.
    else:
        for i in range(num, 0):
            print(i)

printNumber(-5)
printNumber(5)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM