Nihrion
Nihrion

Reputation: 5

How to keep asking for a valid input in python without stopping the code

Hi I am trying to have my code ask for an input until the correct value had been entered. if the input is correct then it proceeds to perform other tasks. Otherwise the user is to be prompted again. I tried using Try/Except but cannot get it right. I have this:

while True:
try:
    radius = float(input("Enter radius: "))
    if radius > 1 and radius < 0:
        
        break
        print("Radius must be between 0 and 1. Try again: \n")

except Exception as e:
    print(e)
finally:
    print( 'ok lets go on')
    ### more tasks are performed and stuff ###

In order to continue the user must input a float radius between 0 and 1. otherwise it keeps asking. Im still new so thanks for your patience!

Upvotes: 0

Views: 576

Answers (5)

HussinMhmode
HussinMhmode

Reputation: 35

while True:
    try:
        radius = float(input("Enter radius: "))
        if radius < 1.0 and radius > 0.0:
            break
            print("Radius must be between 0 and 1. Try again: \n")

    except Exception as e:
        print(e)
    finally:
        print('ok lets go on')
        ### more tasks are performed and stuff ###

It will be working good just correct the operators

before

  if radius > 1 and radius < 0 :

after

 if radius < 1.0 and radius > 0.0:

output

Enter radius: 0.9
ok lets go on

Process finished with exit code 0

Upvotes: 1

Dmitry Grekov
Dmitry Grekov

Reputation: 688

There're several problems in the code.

First, there's an indentation: the code to be repeated in the cycle should be additionally indented (one level deeper then the while keyword.

Second, the condition you check is incorrect: there should rather be or operator:

if radius > 1 and radius < 0:

(this condition is never satisfied as the value can't be less then 0 and grater than 1 simultaneously).

Third, you should better decide when to exit the cycle. You can do it explicitly by using break keyword after yiou check that the value is correct. In you current code you are trying to do it if the value doesn't meet your [incorrect] condition.

In case you need to immediately start the next loop, use the continue keyword.

And also I can't see why you need the finally block there.

So the code should look smth. like this:

while True:
    try:
        radius = float(input("Enter radius: "))
        if radius > 1 or radius < 0:
            print("Radius must be between 0 and 1. Try again: \n")
            continue 
            
        break
    except Exception as e:
        print(e)
        continue

print( 'ok lets go on')
### more tasks are performed and stuff ###

The last continue keyword is not necessary in this case as the cycle will repeat automatically after the last statement, but I've left it there for the code readability.

Upvotes: 0

Rumira Daksith
Rumira Daksith

Reputation: 194

your iterations and if conditions are wrong. between 0 to 1 mean we cant get 0 as input we must reject it. and we must reject invalid inputs like strings

radius = float()
    while True:
        try:
            radius = float(input("Enter radius: "))
            if ((radius >= 1) or (radius <= 0)):
                print("Radius must be between 0 and 1. Try again: \n")
                continue
            else:
                break
        except Exception as ex:
            print("Float required !")
            print(ex)
            
    
    print("radius" +  str(radius))

Upvotes: 0

Rishabh Semwal
Rishabh Semwal

Reputation: 348

while True:
    radius = float(input("Enter radius: "))
    if radius > 1 or radius < 0:
        print("Radius must be between 0 and 1. Try again: \n")
        continue
    else:
        print('ok lets go on')

It does what you say. But please let me know in the comments when you want to break the code.

Upvotes: -1

Johnny John Boy
Johnny John Boy

Reputation: 3202

There are a few things wrong. It's not possible for a number to be both <0 and >1, finally will always be run regardless of the result of the try and except blocks, you are not breaking out of the while loop so end up running forever.

Something like this would do it:

while True:
    try:
        radius = float(input("Enter radius: "))
        if radius > 1 or radius < 0:
            print("Radius must be between 0 and 1. Try again: \n")
        else:
            break
    except Exception as e:
        print(e)

print( 'ok lets go on')

Upvotes: 1

Related Questions