2

I'm stuck on a question for an assignment for school. Am i missing something with python

I am running my input's in a while loop and when a value error is thrown. I want to return to the last question asked. but every time it restarts from the beginning of the code.

while True:
    try:
        start_value = int(input('Enter a start value (Default: 0): ') or 0)
        end_value = int(input('Enter a end value: '))
        step_value = int(input('Enter step value (Default: 1): ') or '1')

    except ValueError:
        print('Numeric value only!')
        continue
    else:
        for x in range(start_value, end_value, step_value):
           print(str(x), end=' ')
    break

can anyone point me in the right direction. I have looked all over and only examples i can find are while loops only with one input in the try statement

Maverick
  • 107
  • 6

1 Answers1

1

How about using if to control the flow?

i = 0
while True:
    try:
        if i==0:
            start_value = int(input('Enter a start value (Default: 0): ') or 0)
            i=1
        if i==1:    
            end_value = int(input('Enter a end value: '))
            i=2
        if i==2:
            step_value = int(input('Enter step value (Default: 1): ') or '1')

    except ValueError:
        print('Numeric value only!')
        continue
    else:
        for x in range(start_value, end_value, step_value):
           print(str(x), end=' ')
    break
LocoGris
  • 4,432
  • 3
  • 15
  • 30