我无法获取代码以打印出正确的信息。我只是永远打印一个数字

时间:2019-11-20 17:15:53

标签: 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)

6 个答案:

答案 0 :(得分:1)

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

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)

答案 1 :(得分:1)

说实话,您的代码有很多问题,我会尝试解决其中的一些问题。

  • 首先,按照惯例,大写字母保留用于类,所以我替换了大写字母。

  • 第二,尝试对变量赋值使用更简单的表示法,似乎您感到困惑。

  • 第三,您试图同时将整数转换为整数和列表/数组,因此您永远都无法得到总和,因此我将其分为两部分:

话虽如此,这保留了代码的逻辑性并起作用:

integer = int(input('Please enter an integer: '))
print ('The sequence is:')
sum_int= integer
if integer < 0:
    while integer != 0:

        new_int = integer + 1
        print (new_int)
        sum_int+= new_int
        integer =  new_int
    print ('The sum of the sequence is ',sum(int_list))
elif integer > 0:

    while integer != 0:
        new_int = integer - 1
        sum_int+= new_int
        print (new_int)
        integer = new_int
    print ('The sum of the sequence is ',sum(int_list))

elif integer == 0:
    print (integer)
    print ('The sum of the sequence is ',integer)

在循环的每次迭代中,您将new_int添加到sum_int,然后从第一个new_int值开始integer,这样就可以得出循环结束。

答案 2 :(得分:1)

正如评论中提到的那样,您有几个语法错误和一些逻辑错误。

Integer = int(input('Please enter an integer: '))
print('The sequence is:')
sum = 0
if Integer < 0:
    while Integer != 0:
        sum += Integer
        print(Integer)
        Integer = Integer + 1
elif Integer == 0:
    print (Integer)
else:
    while Integer != 0:
        sum += Integer
        Integer -= 1
        print(Integer)

print('The sum of the sequence is ', sum)

答案 3 :(得分:0)

我想这就是您想要做的:

Integer = int(input('Please enter an integer: '))
print ('The sequence is:')
Sum = Integer
print (Integer)
if Integer < 0:
    while Integer != 0:
        Integer += 1
        print (Integer)
        Sum += Integer
    print ('The sum of the sequence is ', Sum)
elif Integer == 0:
    print (Integer)
    print ('The sum of the sequence is ', Integer)
else:
    while Integer != 0:
        Integer -= 1
        print (Integer)
        Sum += Integer
    print ('The sum of the sequence is ', Sum)

所以输出将是:

Please enter an integer: -6
The sequence is:
-6
-5
-4
-3
-2
-1
0
The sum of the sequence is  -21

答案 4 :(得分:0)

通过写Integer =+1,您说Integer变量为+1并且未递增。请查看此主题以获取更多信息:The difference between '+=' and '=+'? 正确的代码是:

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)

您还可以简化打印变量。 警告名称变量,请勿使用大写字母

答案 5 :(得分:0)

因此,您对+ =运算符的操作感到困惑。你不必放 完全New_int = Integer =+ 1。您的意思是New_int = Integer + 1

尽管如此,它完全不需要新变量New_int就能更好地工作:

以下内容适用于负数序列。

print ('The sequence is:')
int_list = []
if Integer < 0:
    while Integer != 0:
        print(Integer)
        int_list.append(Integer)
        Integer += 1
    print ('The sum of the sequence is ',sum(int_list))

(请注意我在代码外放置的总和和列表,您也可以使用变量current_sum = 0来添加它,并在每次迭代中将当前Integer添加到其中。

以上的输出是Integer = -6的以下内容:

The sequence is:
-6
-5
-4
-3
-2
-1
The sum of the sequence is  -21

请听有关将变量全部小写的建议。