Jump to content

Question about functions in python.

OrangeSunshine
Go to solution Solved by Slottr,

Nope, thats fine.

I'm working on getting the hang of python. One of the exercises in the book I'm using wants me to create a function to print a city and its location. This is my code.

def describe_city(city, country = "America"):
    '''Describes a City and Location'''
    print (f'{city.title()} is in {country.title()}')

describe_city('memphis')

This is the answer provided in the book.

def describe_city(city, country='chile'):
    """Describe a city."""
    msg = f"{city.title()} is in {country.title()}."
    print(msg)
    
describe_city('santiago')

Both work and give the same results, so I am wondering what is the point in storing a message in a variable? Is it a bad idea to include everything in a print call like I did?

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×