Home > Blockchain >  how to go to a line of code outside a function when a condition in the function is true in python
how to go to a line of code outside a function when a condition in the function is true in python

Time:10-09

I have this confirm() function defined...In confirm() there's an "elif" condition embedded in a "while" loop. When the elif statement is true, I want the interpreter to print the statement and go to the "phone_number" input prompt outside of the while loop and the function itself. The input prompt is declared before this function is called.

def confirm():
    while True:
        response = input("Type 'y' to confirm and 'n' to enter the number again>>>")
        if response == "y":
            print("Your phone number has been verified")
            break
        elif response == "n":
            print("Let's try that again")
        else:
            print("Kindly enter a valid response")


digits_map = {
    "0" : "Zero",
    "1" : "One",
    "2" : "Two",
    "3" : "Three",
    "4" : "Four",
    "5" : "Five",
    "6" : "Six",
    "7" : "Seven",
    "8" : "Eight",
    "9" : "Nine"
}

phone_number = input("Enter your phone number>>>")
words = ""
for ch in phone_number:
    words  = digits_map.get(ch, "non-numeric character!")   " "
print(f"Confirm your phone number {phone_number} in words is {words}")


confirm()

CodePudding user response:

You can create one more function to ask for phone number, and you also need to add break for response = "n" as well.

def confirm():
    while True:
        response = input("Type 'y' to confirm and 'n' to enter the number again>>>")
        if response == "y":
            print("Your phone number has been verified")
            break
        elif response == "n":
            print("Let's try that again")
            phone()
            break
        else:
            print("Kindly enter a valid response")


digits_map = {
    "0" : "Zero",
    "1" : "One",
    "2" : "Two",
    "3" : "Three",
    "4" : "Four",
    "5" : "Five",
    "6" : "Six",
    "7" : "Seven",
    "8" : "Eight",
    "9" : "Nine"
}

def phone():
    phone_number = input("Enter your phone number>>>")
    words = ""
    for ch in phone_number:
        words  = digits_map.get(ch, "non-numeric character!")   " "
    print(f"Confirm your phone number {phone_number} in words is {words}")
    confirm()

phone()

CodePudding user response:

You can actually not go outside of a function, instead prompt the user to input the number in the function itself in the elif response == "n": clause.

Like, you should do this change:

elif response == "n":
    print("Let's try that again")
    phone_number = input("Enter your phone number>>>")
    print(f"Confirm your phone number {phone_number} in words is {words}")

CodePudding user response:

Python won't let you just jump outside of the function like that and go to a specific line in your code. The flow of your code just needs to be slightly different. Here's how I would do it.

digits_map = {
    "0" : "Zero",
    "1" : "One",
    "2" : "Two",
    "3" : "Three",
    "4" : "Four",
    "5" : "Five",
    "6" : "Six",
    "7" : "Seven",
    "8" : "Eight",
    "9" : "Nine"
}


def askphone():
    phone_number = input("Enter your phone number>>>")
    words = ""
    for ch in phone_number:
        words  = digits_map.get(ch, "non-numeric character!")   " "
    print(f"Confirm your phone number {phone_number} in words is {words}")

    # now let's confirm it with the user and potentially go back to this function
    confirm()


def confirm():
    while True:
        response = input("Type 'y' to confirm and 'n' to enter the number again>>>")
        if response == "y":
            print("Your phone number has been verified")
            break
        elif response == "n":
            print("Let's try that again")
            askphone()  # call the phone function that will call this confirm again
            break  # make sure to break when done (or we will have a ton of loops)
        else:
            print("Kindly enter a valid response")


# it's good practice to have this statement
# all this means is that if we run THIS script then run whatever is 
# inside this if statement, but if we IMPORT this script then don't
# run what is inside the if. This is super useful for when you want
# to just use the functions this script in another script but not run the askphone()
if __name__ == '__main__':
    askphone()

CodePudding user response:

I think you want something like this:

def confirm():
    while True:
        response = input("Type 'y' to confirm and 'n' to enter the number again>>>")
        if response == "y":
            print("Your phone number has been verified")
            return True
        elif response == "n":
            print("Let's try that again")
            return False
        else:
            print("Kindly enter a valid response")


digits_map = {
    "0" : "Zero",
    "1" : "One",
    "2" : "Two",
    "3" : "Three",
    "4" : "Four",
    "5" : "Five",
    "6" : "Six",
    "7" : "Seven",
    "8" : "Eight",
    "9" : "Nine"
}

while(True):
    phone_number = input("Enter your phone number>>>")
    words = ""
    for ch in phone_number:
        words  = digits_map.get(ch, "non-numeric character!")   " "
    print(f"Confirm your phone number {phone_number} in words is {words}")

    if(confirm()):
        break
  • Related