python - Python 循环 N 到 0 或 -N 到 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)
推荐阅读
- java - Java 桌面应用程序中的 Azure 多因素身份验证
- ionic-framework - IONIC-V3:在继续执行代码之前等待页面弹出
- python - 如何自动填充 One2many
- python - Flask 应用程序在 Heroku 上失败:没有名为 app 的模块
- design-patterns - 责任链设计模式如何用于处理 Windows 中的鼠标单击和键盘事件?
- css - 如何使用 CSS 将所有内容对齐在一行中
- html - 不同 Jinja2 模板的不同背景图片
- tensorflow - Tensorflow 密集层比 keras 顺序层差
- python - 在pygame中实现定时器
- android - 检查媒体播放器