[Tutor] Python programming question related to Return/Print statement
Brenda Ives
brendap at marketamerica.com
Mon Aug 17 08:53:54 EDT 2020
Good morning,
While I am still learning python, I've been programming for a very long time. Some of what the issues with this is logical knowing how computers work. While we know that "" means you don't print anything computers are literal and they will print a space and a comma when you don't want that to occur. You need to check for empty values and skip them. This was actually a good learning lesson for me so I had to try it. Here is what my attempt arrived at.
def format_name(first_name, last_name):
# return "Name: " + first_name + ", " + last_name
if last_name is '':
return "Name: " + first_name
elif first_name is '':
return "Name: " + last_name
else:
return "Name: " + last_name + ", " + first_name
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
When I ran it, here is the result.
Name: Hemingway, Ernest
Name: Madonna
Name: Voltaire
Name:
-----Original Message-----
From: Tutor <tutor-bounces+brendap=marketamerica.com at python.org> On Behalf Of Mats Wichmann
Sent: Friday, August 14, 2020 2:53 PM
To: tutor at python.org
Subject: Re: [Tutor] Python programming question related to Return/Print statement
On 8/14/20 11:45 AM, eric grunfeld wrote:
> Good Afternoon Everyone:
>
> I have been seeking to rectify my mistake in running this program via
> Python.
>
> Here it is:
>
> def format_name(first_name, last_name):
> return "Name " + first_name + ", " + last_name
> return string
the second statement will never be reached, so drop it.
> However, after running the program, this has been the response:
> Incorrect
>
> Not quite, format_name('Ernest', 'Hemingway') returned Name Ernest,
> Hemingway, should be Name: Hemingway, Ernest.
>
> It appears to be how I have programmed my return statement.
it seems whatever grading tool you're entering this into is expecting a colon after the word Name, so you should include that when you construct the string.... it's possible from what you pasted that it also expected a terminating period.
So it appears a question of reading the problem specification very precisely.
_______________________________________________
Tutor maillist - Tutor at python.org<mailto:Tutor at python.org>
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list