1

A function called month_days, that receives the name of the month and the number of days in that month as parameters. Adapt the rest of the code so that the result is the same. Confirm your results by making a function call with the correct parameters for both months listed.

def month_days(month, days):
   month__name = str(month)
   month__days = int(days)
   print(month__name + "has" + month__days + "days" )

month_days(June,30)

Giving following error

NameError: name 'June' is not defined
dejanualex
  • 3,872
  • 6
  • 22
  • 37
Kamal Adlan
  • 21
  • 1
  • 4
  • are your parameters defined elsewhere in the code for example in a list, ex. months = ['June', 'July', 'August'] ? – Mahmoud Abdel-Rahman Jun 07 '20 at 14:32
  • 1
    You haven't defined the variable June anywhere. If you meant to pass in the *string* June, then it needs to be in quotation marks. Of course, then you have to ask yourself, why are you converting a string to a string and an integer to an integer, and can you add a string to an integer? (Answer: not in Python) – kindall Jun 07 '20 at 14:34
  • [Can I answer my own question?](https://stackoverflow.com/help/self-answer) – wwii Jun 07 '20 at 14:39
  • @Kamal please avoid writing "thank you" comments. You can always show your appreciation by upvoting the answer or even accepting it :) – Sabito stands with Ukraine Oct 25 '20 at 20:48
  • Does this answer your question? [Python NameError: name is not defined](https://stackoverflow.com/questions/14804084/python-nameerror-name-is-not-defined) – philipxy Oct 21 '22 at 07:14

4 Answers4

3

First, let's try to understand what is NameError. NameError in Python often refers to execution or calling an object which is not found or not instantiated.

Here in you case month_days(June, 30), what is this June? Is it a variable already defined?

My guess if you are trying to pass June string as input, if so try to do the following

month_days("June", 30) # works
month_days('June', 30) # also works

In Python generally, all string inputs are enclosed in single or double-quotes.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
sam
  • 1,819
  • 1
  • 18
  • 30
0

You are passing June as value to a function, hence, it should be as string i.e with singe/double quotes. One more issue i have observed with you code is you are adding integer (month_days) with string. Hence, corrected by converting input days as string i/o integer. Please find below code.

def month_days(month, days):
    month__name = str(month)
    month__days = str(days)
    print( month__name+" has " + month__days + " days" )
month_days('June',30)
Tarun Kose
  • 51
  • 1
0

This can work too:

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

month_days("June", 30)
month_days("July", 31)
skibee
  • 1,279
  • 1
  • 17
  • 37
0

Just a couple of fixes, you are really close.

  1. Within the print statement inside the body, you will want to ensure an explicit conversion takes place on month__days by using the str() function.

  2. You will want the appropriate spaces and a "." at the end of your print statement. This is just what the coursera autograder expects, so it has to be precise.

  3. Lastly, as others have mentioned, you will want to put quotations around June and July.

def month_days(month, days):   
    month__name = str(month)  
    month__days = int(days)  
    print(month__name + " has " + str(month__days) + " days." )  
    
month_days('June', 30)  
month_days('July', 31)

Hope that helps! :)

Freddy Mcloughlan
  • 4,129
  • 1
  • 13
  • 29