0

I am trying to get this code to print numbers descending or ascending to 0 based on user input of an integer. In its current form it is repeatedly printing -1 or 1 depending on positive or negative input. I want the output to look similar to this.
Please enter an integer:
-6
The sequence is:
-6
-5
-4
-3
-2
-1
0
The sum of the sequence is -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)
Caleb Kunz
  • 25
  • 5
  • Take a look at `Integer = Integer =+ New_int`. This is not actually adding anything, instead you are assigning `Integer` the positive value of `New_int`. So if `New_int` is 12, `Integer` also becomes 12. Same for when you are doing the subtraction, you are assigning the negative value. Instead you need `New_int -= 1` and `Integer += New_int`. – Chrispresso Nov 20 '19 at 17:22

6 Answers6

1

Your syntax is incorrect, try running this instead:

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

to be honest there's a lot of problems with your code, I'll try to help with some of them.

  • First, capital letters are, by convention, reserved for classes, so I replaced those.

  • Second, try to use simpler notation for variable asignation, it seems you got confused.

  • Third, you were trying to turn integer into an integer and a list/array at the same time, so you would have never gotten the sum printed, so I splitted that into two:

That being said, this preserves the logic of you code and works:

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)

In each iteration of the loop your adding new_int to sum_int and you start new_int with the first integer value, so that'll give you the sum at the end of the loop.

Juan C
  • 5,846
  • 2
  • 17
  • 51
1

As was mentioned in the comments you have several syntax errors and some logical errors.

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)
Michael Scott
  • 186
  • 1
  • 7
0

I guess this is what you are trying to do:

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)

So the output will be:

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

By writing Integer =+1 you say that the Integer variable is +1 and not incremented check this topic for more informations : The difference between '+=' and '=+'? The right code is :

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)

You can also simplify print variables. Warning to the name variables, don't use capitals letters

ines
  • 1
  • 1
0

So you are confusing what the += operator does. you don't have to put New_int = Integer =+ 1 at all. What you mean is New_int = Integer + 1

Nontheless, it works better without a new variable New_int at all:

The following works for the sequence in negative numbers.

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))

(note the sum and the list I put outside of the code, you can also make it with a variable current_sum = 0, and add the current Integer to it each iteration.

The output for the above is the following for Integer = -6:

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

Do listen to the suggestion of making your variables all lowercase.