-2

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Simsii
  • 3
  • 2
  • Did you define the function before, or after, the code that calls it? After does you no good. – jasonharper Jun 06 '22 at 23:25
  • I declared it before – Simsii Jun 06 '22 at 23:30
  • In your own words, where the code says `while not quit:`, what do you expect that to mean? Will the condition be satisfied the first time the code reaches this point? Why? – Karl Knechtel Jun 06 '22 at 23:37
  • See also: https://stackoverflow.com/questions/39983695/what-is-truthy-and-falsy-how-is-it-different-from-true-and-false – Karl Knechtel Jun 06 '22 at 23:39
  • Welcome to Stack Overflow. Please read [ask] and note that this is **not a discussion forum**. We are not interested in conversational language or discussion of your skill level, attitude towards learning etc.; but only in clear, specific questions. I [edit]ed the question to show how it's done. In the future, though, please try to show [complete](https://meta.stackoverflow.com/questions/359146) error messages. – Karl Knechtel Jun 06 '22 at 23:40

2 Answers2

0

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
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
UnityBug
  • 16
  • 1
  • 2
    Welcome to Stack Overflow. Please read [answer] and try to write answers in proper English sentences, without slang such as "ur". – Karl Knechtel Jun 06 '22 at 23:39
0

In python, every string except empty string ('' and "") are considered True. The codition not quit, evaluates to not True, evaluates to False. So the while loop, does not runs.

You are getting NameError, because, firstName are lastName are not declared, till running the statement fullName = formatted_name(firstname,lastname).

The correct version of the above code is:

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


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