Defining month_days to show number of days in each month

Viewed 188

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)
1 Answers

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))
Related