0

Super Duper Beginner: Why is my code returning the correct print but also None. any other comments welcome:)

def format_name(first_name, last_name): if(first_name and last_name): print("Name: " + last_name + ", " + first_name) elif(first_name): print("Name: " + first_name) elif(last_name): print("Name: " + last_name) else: return string 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

17th Jul 2022, 7:13 PM
Butters
Butters - avatar
2 Réponses
+ 3
print(format_name()) is being used inconsistently. In some cases format_name prints output and then returns nothing. In one case it doesn't print, but it returns a string. Then the print function that called it in the main code prints whatever is returned - either None, or the string. Instead of printing within format_name, it should always return a string and let the main code do the printing.
17th Jul 2022, 7:54 PM
Brian
Brian - avatar
+ 1
Pls save code and share link here.. Identation is much important in python.
17th Jul 2022, 7:16 PM
Jayakrishna 🇮🇳