Python Forum
do not understand. can help?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
do not understand. can help?
#1
#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?
buran write Apr-08-2021, 05:02 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Do you understand functions? If not, what specifically don't you understand?
Reply
#3
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".
Reply
#4
You need to provide more context.
Reply
#5
**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. 
buran write Apr-08-2021, 05:04 AM:
Please, don't provide answers for those without effort on their part
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
It's not a good idea to do their work for them :(.
buran likes this post
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020