简体   繁体   中英

Why won't my program run the code all the way to the last print call? (Python)

I am a newcomer to programming, still learning and probably missing some easy mistake here. The code runs perfectly fine, until it is supposed to "print the last print" (printf"\\nHello, {formatted_name}!")) , which it does not do unfortunately... As I said, I am probably missing something obvious here, I'd appreciate the help. I have tried both def...(): and def...(f_name, l_name) none seems to work the way I wish.

def get_formatted_name(f_name, l_name): / def get_formatted_name():

    while True:
        print("\nPlease tell me your name:")
        print("(enter 'q' at any time to quit)")
        
        f_name = input("First name: ")
        if f_name == 'q':
            break
        l_name = input("Last name: ")
        if l_name == 'q':
            break

        formatted_name = get_formatted_name(f_name, l_name)
        print(f"\nHello, {formatted_name}!")

I'm not much of an expert with Python, but here's my two cents:

  1. formatted_name = get_formatted_name(f_name, l_name) starts the whole process again, which means it never gets to the last print statement. It loops the same function over and over until the user enters "q" as either their first or last name, in which case it doesn't reach print in any possible case. Notice that the function asking for the user's input and the one You're trying to print out the user's name with are the same - recursion.
  2. while True: seems pointless to me, unless you're practicing and learning Python. The code keeps asking the user for their first and last names, even if they give them both. Only way to break out of the while loop is to enter "q" as either name.

My fix, if you could call it that:

with while loop:

def get_formatted_name():
    f_name = ""
    l_name = ""

    while not f_name and not l_name:
        print("\nPlease tell me your name:")
        print("(enter 'q' at any time to quit)")
    
        f_name = input("First name: ")
        if f_name == 'q':
            break
        l_name = input("Last name: ")
        if l_name == 'q':
            break

    print(f"\nHello, {f_name} {l_name}!")

without while , the very basic for learning and practicing:

def get_formatted_name():
    print("\nPlease tell me your name:")
    print("(enter 'q' at any time to quit)")
    
    f_name = input("First name: ")
    l_name = input("Last name: ")

    print(f"\nHello, {f_name} {l_name}!")

Using while version is probably better, since You can be assured the user enters both name or can opt out of entering either.

When you run formatted_name = get_formatted_name(f_name, l_name) you are in fact calling the function you are in from within itself. it is what's called a recursion . to fix your issue I believe you want to replace the line with:

formatted_name = f_name + " " + l_name

or another way using format :

formatted_name = "{} {}".format(f_name, l_name)

using fstring :

formatted_name = f"{f_name} {l_name}"

You might want to read up on recursion to understand it better

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM