python - 我无法让我的代码打印出正确的信息。我只是永远打印一个数字
问题描述
我试图让此代码根据用户输入的整数打印降序或升序到 0 的数字。在其当前形式中,它根据正负输入重复打印 -1 或 1。我希望输出看起来与此类似。
请输入整数:
-6
序列为:
-6
-5
-4
-3
-2
-1
0
序列之和为-21。
Integer = int(input('Please enter an integer: '))
print ('The sequence is:')
if Integer < 0:
while Integer != 0:
New_int = Integer =+ 1
print (New_int)
Integer = Integer =+ New_int
print ('The sum of the sequence is ',Integer)
elif Integer == 0:
print (Integer)
print ('The sum of the sequence is ',Integer)
else:
while Integer != 0:
New_int = Integer =- 1
print (New_int)
Integer = Integer =+ New_int
print ('The sum of the sequence is ',Integer)
解决方案
您的语法不正确,请尝试运行它:
Integer = int(input('Please enter an integer: '))
print ('The sequence is:')
if Integer < 0:
while Integer != 0:
Integer += 1
print (Integer)
print ('The sum of the sequence is ',Integer)
elif Integer == 0:
print (Integer)
print ('The sum of the sequence is ',Integer)
else:
while Integer != 0:
Integer -= 1
print (Integer)
print ('The sum of the sequence is ',Integer)
推荐阅读
- jquery - 错误未定义的偏移量:多次插入 Laravel 6 中的 5
- swift - “无法将模块‘金属’加载为‘金属’”
- unix - Unix中date命令的含义和输出
- c++ - Libwesockets.h:lws_write 的问题:C++ 字符串到 C 的转换并发送
- javascript - 如何在我们的反应应用程序中获取 JWT cookie,如何检查用户是否登录我无法找到如何处理我的反应应用程序会话
- css - 如何在 CSS 中为闪烁的文本光标设置动画和样式?
- node.js - nodejs FFMPEG添加多个输入不起作用
- java - 如何在子片段中登录Facebook,它是片段内的片段 - AndroidX
- java - 有没有办法将 EncryptedSharedPreference 与 PreferenceScreen 集成?
- html - 为什么浏览器不显示网站更改html