Skip to content
Advertisement

Python counting up (1+2+3 etc.) until input value

How to keep counting up (1+2+3) until user input value?

This is my code so far,

until = int(input("Keep counting until: "))

x = 1

while until >= x:

    x = x + 1

print(x)

I can’t figure out how to keep increasing the value by one at a time… the goal should be

e.g if input value was 2 to print 3 and if 10, 10 and 18, 21.

Advertisement

Answer

The problem with your code is you always do x + 1, but you want x + i where i increments each time. Then you want to stop when x < until not when x <= until.

This will do what you need:

until = int(input("Keep counting until: "))

x = 0
i=0
while x < until:
    i+=1
    x = x + i

print(x)

Example output:

Keep counting until: 18
21
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement