Could someone help me with this function? Systems always: Incorrect Not quite, format_name('', '') returned Name: , should be
Question:
This is my code
def format_name(first_name, last_name):
string = ('Name: ' +last_name+", "+first_name)
if first_name =="":
return ("Name: " + last_name)
elif last_name =="":
return("Name: "+ first_name)
elif first_name == "" and last_name == "":
return ""
else:
return string
print(format_name("Ernest", "Hemingway"))
# Should return the string "Name: Hemingway, Ernest"
print(format_name("", "Madonna"))
# Should return the string "Name: Madonna"
print(format_name("Voltaire", ""))
# Should return the string "Name: Voltaire"
print(format_name("", ""))
# Should return an empty string
Name: Hemingway, Ernest
Name: Madonna
Name: Voltaire
Name:
but the system always showed my result "incorrect"
Not quite, format_name(”, ”) returned Name: , should be .
Answers:
We don’t see any error in your code. But, one thing is notable that is there is an extra space between first_name and last_name.
Try to remove it and check it again.
string = ('Name: ' +last_name+","+first_name
There seems to be just one logical error, and it is solved by thinking how computer process your code from left to right and from up to down. Now if you give empty strings as arguments to your function’s both parameters (first_name, last_name), first it evaluates if the first_name is an empty string “”.
And as we can see the first if conditional turns out to be true, and this provokes the problem, because the first conditional in your function body will execute the if block, meaning that it will return a string starting as (“Name: “ + last_name) WITHOUT evaluating if the last parameter (last_name) is an empty string as well.
So, first you should test the both parameters if for empty strings.
Like so:
def format_name(first_name, last_name):
string = ('Name: ' +last_name+", "+first_name)
if first_name == "" and last_name == "":
return ""
elif first_name =="":
return ("Name: " + last_name)
elif last_name =="":
return("Name: "+ first_name)
else:
return string
def format_name(first_name, last_name):
if first_name != '' and last_name != '':
string = 'Name:'+' ' + last_name, first_name
elif first_name != '' and last_name == '' or first_name == '' and last_name != '':
string = 'Name:'+' ' + first_name + last_name
else:
string = last_name, first_name
return string
I dont know if this is the right explanation but remenber codes is read from top to bottom by writing the first condition with your parameters empty you are telling the interpreter that as soon as this condition is met it has to return an empty string and you accomplish that with "return("")".
The system tells you that is incorrect because the condition says "if both first and last names are empty return an EMPTY STRING" meaning nothing, in your code you still returning "Name: ".
I hope this helps.
def format_name(first_name, last_name):
string = ('Name: ' + last_name + "," + " " + first_name)
if first_name == "" and last_name == "":
return ("")
elif first_name == "":
return ('Name: ' + last_name)
elif last_name == "":
return ('Name: ' + first_name)
return string
print(format_name("Ernest", "Hemingway"))
# Should return the string "Name: Hemingway, Ernest"
print(format_name("", "Madonna"))
# Should return the string "Name: Madonna"
print(format_name("Voltaire", ""))
# Should return the string "Name: Voltaire"
print(format_name("", ""))
This is my code
def format_name(first_name, last_name):
string = ('Name: ' +last_name+", "+first_name)
if first_name =="":
return ("Name: " + last_name)
elif last_name =="":
return("Name: "+ first_name)
elif first_name == "" and last_name == "":
return ""
else:
return string
print(format_name("Ernest", "Hemingway"))
# Should return the string "Name: Hemingway, Ernest"
print(format_name("", "Madonna"))
# Should return the string "Name: Madonna"
print(format_name("Voltaire", ""))
# Should return the string "Name: Voltaire"
print(format_name("", ""))
# Should return an empty string
Name: Hemingway, Ernest
Name: Madonna
Name: Voltaire
Name:
but the system always showed my result "incorrect"
Not quite, format_name(”, ”) returned Name: , should be .
We don’t see any error in your code. But, one thing is notable that is there is an extra space between first_name and last_name.
Try to remove it and check it again.
string = ('Name: ' +last_name+","+first_name
There seems to be just one logical error, and it is solved by thinking how computer process your code from left to right and from up to down. Now if you give empty strings as arguments to your function’s both parameters (first_name, last_name), first it evaluates if the first_name is an empty string “”.
And as we can see the first if conditional turns out to be true, and this provokes the problem, because the first conditional in your function body will execute the if block, meaning that it will return a string starting as (“Name: “ + last_name) WITHOUT evaluating if the last parameter (last_name) is an empty string as well.
So, first you should test the both parameters if for empty strings.
Like so:
def format_name(first_name, last_name):
string = ('Name: ' +last_name+", "+first_name)
if first_name == "" and last_name == "":
return ""
elif first_name =="":
return ("Name: " + last_name)
elif last_name =="":
return("Name: "+ first_name)
else:
return string
def format_name(first_name, last_name):
if first_name != '' and last_name != '':
string = 'Name:'+' ' + last_name, first_name
elif first_name != '' and last_name == '' or first_name == '' and last_name != '':
string = 'Name:'+' ' + first_name + last_name
else:
string = last_name, first_name
return string
I dont know if this is the right explanation but remenber codes is read from top to bottom by writing the first condition with your parameters empty you are telling the interpreter that as soon as this condition is met it has to return an empty string and you accomplish that with "return("")".
The system tells you that is incorrect because the condition says "if both first and last names are empty return an EMPTY STRING" meaning nothing, in your code you still returning "Name: ".
I hope this helps.
def format_name(first_name, last_name):
string = ('Name: ' + last_name + "," + " " + first_name)
if first_name == "" and last_name == "":
return ("")
elif first_name == "":
return ('Name: ' + last_name)
elif last_name == "":
return ('Name: ' + first_name)
return string
print(format_name("Ernest", "Hemingway"))
# Should return the string "Name: Hemingway, Ernest"
print(format_name("", "Madonna"))
# Should return the string "Name: Madonna"
print(format_name("Voltaire", ""))
# Should return the string "Name: Voltaire"
print(format_name("", ""))