Home > front end >  How to break a function to a specific spot after a failed test
How to break a function to a specific spot after a failed test

Time:01-23

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?

CodePudding user response:

Try using the combination of a while True and `return statement my bro!

def joe():
    while True:
        print("What is your name? ")
        name = input()
        if name != "Joe":
            continue

        while True:
          print("What is your password? (it is a fish) ")
          answer = input()

          if answer == "swordfish":
            print("nice job, Joe")
            return
    
joe()

CodePudding user response:

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()

CodePudding user response:

Add another while loop for the password part.

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

  •  Tags:  
  • Related