Python Forum

Full Version: do not understand. can help?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
#replace this code with function
june_days = 30
print("June has" + str(june_days) + "days")
july days = 31
print("July has" + str(july days) + "days")
do not understand. Can help?
Do you understand functions? If not, what specifically don't you understand?
I think your teacher means: create a function that takes 2 arguments: a month name and a number of days. The function should then print: "{month_name} has {num_days} days".
You need to provide more context.
**code removed


def is the Keyword for function definition. After this come the name of the function. Then in parentheses the arguments of the function and then the colon.

Calling a function:
pint("Hello World")
print is a function.

To call print_month:
print_month("June", 30)
The last piece of code is the f-string:
text1 = "{0} has {1} days".format(month_name, num_days)
text2 = "{} has {} days".format(month_name, num_days)
text2 = "{name} has {days} days".format(name=month_name, days=num_days)
text3 = f"{month_name} has {num_days} days"

# but never do:

value1 = 42
text4 = "Something " + str(value1) + " something else."
# value1 is an int and must be converted to a str, before
# it's concatenated with "Something ". This is not very handy.
# It's also not good readable

# use instead the format method or f-strings. 
It's not a good idea to do their work for them :(.