[SOLVED] How to break a function to a specific spot after a failed test

Issue

def joe():
    while True:
        name = ""
        answer = ""
        print("What is your name? ")
        name = input()
        if name != "Joe":
            continue
        print("What is your password? (it is a fish) ")
        answer = input()
        if answer == "swordfish":
            break
    print("nice job, Joe")
    
     joe()

If I pass the frist statement and type in "Joe" i continue with the function, and all is good. but if I fail the second test, I break the function and get retrieved back to the "what is your name?" part of the function. How can I write a test that will upon failiure retreive me back to the "what is your password"? instead of the name test?

Solution

or try this:

def joe():    
    name = ""
    answer = ""
    print("What is your name? ")
    name = input()
    if name == "Joe":
        print("What is your password? (it is a fish) ")
        answer = input()
    if answer == "swordfish":
        return print("nice job, Joe")
    
    joe()

joe()

Answered By – SimoN SavioR

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published.