Reputation: 13
I am stuck with my code on my assestment. I have passed with sufficient score but I dont like to leave any question on fail- for my own learning. I manaed to get all statements, apart from the last one, where if first and last name are black, the output should be empty. But I am still getting Name - this word shoul dnot be there at all.
Thanks a lot!
# code goes here
if len(first_name)==0:
string="Name: "+last_name
elif len(last_name)==0:
string="Name: "+first_name
elif len(last_name)==0 and len(first_name)==0:
string=""
else:
string="Name: "+last_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("", ""))
# Should return an empty string
Name: Hemingway, Ernest
Name: Madonna
Name: Voltaire
Name:
Upvotes: 0
Views: 725
Reputation: 307
The if-elif-else statement exits on the first met condition. Therefore you should wisely define the order of conditions
Upvotes: 0
Reputation: 81986
It is impossible for you to reach the condition where len(last_name)==0 and len(first_name)==0:
.
This is because either your first conditional: if len(first_name)==0:
or your second conditional: elif len(last_name)==0:
will always be true before reaching that.
The easy solution here is to move that more complex condition as the first one in your if,elif,else chain.
Upvotes: 1