Python Forum
do not understand. can help? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: do not understand. can help? (/thread-33220.html)



do not understand. can help? - Ronno - Apr-07-2021

#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?


RE: do not understand. can help? - ndc85430 - Apr-07-2021

Do you understand functions? If not, what specifically don't you understand?


RE: do not understand. can help? - ibreeden - Apr-07-2021

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".


RE: do not understand. can help? - deanhystad - Apr-07-2021

You need to provide more context.


RE: do not understand. can help? - DeaD_EyE - Apr-07-2021

**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. 



RE: do not understand. can help? - ndc85430 - Apr-07-2021

It's not a good idea to do their work for them :(.