Meg
Meg

Reputation: 23

Defining month_days to show number of days in each month

I'm taking a crash course intro to Python, and stumped trying to define month_days to print the number of days in each month in a sentence saying Month has x days.

This is what I have come up with so far, but I am not sure how to define month:

def month_days(month, days):
    print(month + “ has “ + str(days) + “ days.”)


month_days(June, 30)
month_days(July, 31)

Upvotes: 2

Views: 703

Answers (2)

Muhammad Amjad
Muhammad Amjad

Reputation: 11

def month_days(month, days): print(month + " has " + str(days) + " days.")

month_days("June", 30) month_days("July", 31)

run smoothly, without error but got the following suggestion (or error ).

Here is your output: june has 30 days july has 31 days

Not quite. Let your function do most of the work and just pass the name of the month and the associated days as parameters.

Upvotes: 0

Sylogista
Sylogista

Reputation: 585

There are many things wrong. Remember the general rule: almost everything is case sensitive. Variables month and Month are totally different objects.

Second problem: Print should be lowercase.

Third: you're passing strings to the function, not variables, so you need to quote them. That mean you need to write month_days('June', 30) instead of month_days(June, 30). Without quotes python will look for variable named June, what's not your goal.

At last: correct quote characters. You can use ' and ", but never . The last one, which you were using, is more for writing in natural language than any(!) programming language.

So, code should look like this:

def month_days(month, days):
  print(month + ' has ' + str(days) + ' days.')

month_days('June', 30)
month_days('July', 31)

You can also print it like this:

print(f'{month} has {days} days.')

Or like this:

print('%s has %d days.' % (month, days))

Upvotes: 2

Related Questions