Home > Software design >  Python counting up (1 2 3 etc.) until input value
Python counting up (1 2 3 etc.) until input value

Time:02-12

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.

CodePudding user response:

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

CodePudding user response:

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

x = 0
y = 1

while x <= until: # better to write the condition in the like this
    x  = y
    y  = 1

print(x)
  • Related