Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I need help calling a user defined function in a while loop in python

I defined this function:

def formatted_name(firstName, lastName, middleName=''):
    if middleName:
        full_name = f"{firstName} {lastName} {middleName}"
    else:
        full_name = f"{firstName} {lastName}"
    return full_name.title()

and then tried to use it like so:

prompt = 'Please enter your first and last name below'
prompt += '\nEnter stop to quit'
quit = 'stop'
while not quit:
    print(prompt)
    firstname = input('Enter your first name: ')
    lastname = input('Enter your last name: ')
    if firstname == quit:
        break
fullName = formatted_name(firstname,lastname)
print(fullName)

When I try this, I get a NameError. What is wrong with the code, and how do I fix it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The problem is that firstname and lastname are not defined. It never executes the code inside your while loop because when you convert a string that is not empty to a bool which in this case is "stop" you get True, since you have "not quit" in your while loop then it Will never execute because not quit is False. A while loop with False in it never executes the code inside

>>> bool("stop")
True
>>> not bool("stop")
False
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading