首页 > 解决方案 > 我无法让我的代码打印出正确的信息。我只是永远打印一个数字

问题描述

我试图让此代码根据用户输入的整数打印降序或升序到 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)

标签: python

解决方案


您的语法不正确,请尝试运行它:

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)

推荐阅读