if statement – Python if something repeat function-ThrowExceptions

Exception or error:

I don’t how to make the script repeat this function if the user doesn’t input the correct information.

def installer(x):
if x == "Y" or x == "y":
    print("Ok, Getting On with the install")
    noncustompath()
elif x == "N" or x == "n":
    print("Ok, Cool")
else:
    #Repeat function

Thanks.

How to solve:

In programming, there is a mechanism to return something from a function.

In your case, this could be a simple boolean – true or false.

def installer(x):
    if x == "Y" or x == "y":
        print("Ok, Getting On with the install")
        noncustompath()
        return True
    elif x == "N" or x == "n":
        print("Ok, Cool")
        return True
    else:
        return False

   x = input()
   while not installer(x):
       x = input()

Answer´╝Ü

Use a while loop:

def installer(x):
    if x.upper() == "Y":
        print("Ok, Getting On with the install")
        noncustompath()
        return True
    elif x.upper() == "N":
        print("Ok, Cool")
        return False

installed = False
while not installed:
    user_input = str(input())
    installed = installer(user_input)

Leave a Reply

Your email address will not be published. Required fields are marked *