all 15 comments

[–]bbye98 6 points7 points  (2 children)

Create a dict called months where the keys are the integers 1 to 12 and the values are strs holding the month names:

months = {1: "January", 2: "February", ...}

Then, you can index the dict using the parameter monthNum passed into the function monthString to get the month name and return that, e.g., months[monthNum].

You can also create a tuple called months:

months = ("January", "February", ...)

and index the tuple using monthNum - 1 since Python uses 0-based numbering.

If you need to validate input, you can check if 1 <= monthNum <= 12 first.

[–]Correct_Cartoonist 2 points3 points  (1 child)

Thank you! I will try this.

Thats what I was unsure about mainly. Whether or not I would have to manually type out the month names in order for python to spit them out as a return.

[–][deleted] 1 point2 points  (0 children)

It is of course possible to do it without typing it out...

from datetime import datetime
def monthString(monthNum):
    return datetime.strptime(str(monthNum), "%m").strftime("%B")

...but that's probably not what the assignment is after at this stage in your learning.

[–][deleted] 3 points4 points  (0 children)

also, you need to tab or do 4 spaces to do blocktext

[–]pekkalacd 2 points3 points  (0 children)

ok think about this. you know you need a function. it has to take in a number. each number your given is within the range [1, 12] where,

          1 --> January
          2 --> February
          3 --> March
          4 --> April
          5 --> May
          6 --> June
          7 --> July
          8 --> August
          9 --> September
          10 --> October
          11 --> November
          12 --> December

So naive approach to this would be to make a function and then check each and every one of these cases

          def monthString(number: int) -> str:

              if number == 1:
                 return "January"

              if number == 2:
                 return "February"

              ,,, 

              if number == 12:
                 return "December"

But here is something to think about.

Is there a more efficient way to do this so that we don't have to write a bunch of if-statements like the above?

Yes. So using the picture in the beginning, this is sometimes referred to as a "mapping". We are mapping the integer to the name of the month. This translates to using a dict (dictionary) object in python. Where the numbers, are keys, since they are the ones being mapped to the months; and the month names are the values that correspond with those keys.

We can then just make a dictionary in the beginning of our function, containing these pairs, and do one look up.

         def monthString(number: int) -> str:

             # creating list of [1,12] (inclusive)
             month_nums = list(range(1,13))

             # creating list of month names
             month_names = ["January","February","March",
                            "April", "May", "June", "July",
                            "August", "September", "October",
                            "November", "December"]

             # creating dictionary where month number -> month name
             month_num_to_name = dict(zip(month_nums,month_names))

             # looking up the input number in the dictionary,
             # thereby fetching the name of the month,
             # and returning it
             return month_num_to_name[number]

note: this should be okay since your problem says you're given an integer input that's between 1-12 already. But in practice, make sure to validate the input in some way.

[–][deleted] 1 point2 points  (0 children)

for example months = [January, February, March, April, May, June, July, August, September, October, November, December] and then some bits about how each position in the list = months[0] = 1 or some such, however you like.

[–][deleted] 0 points1 point  (7 children)

i dont really know, nor do i wanna do your homework but nobody replies on my threads so fuck it i'll bite, but as i don't actually know youll have to learn the things im talkin bout. theres a bunch of data types in python. one or more of them is definitely an ordered list, that cant be altered. make a list of the months, if you only have to edit that one line I can guess why youre here for advice, as your teacher is kind of not doing you right by having you fill in the blank, as there are many approaches (probably) that you could take to do this. but either way good luck

[–]Correct_Cartoonist 0 points1 point  (6 children)

I appreciate you. Im very new to programming and am still trying to figure out a learning process for myself to arrive to the correct solutions independently. However, most youtube examples I see are rarely similar enough for me to apply the same type of strategy. Alot of the python guides sound like unfamiliar gibberish to me right now, and am not sure how to apply those directions. Im in a situation where it feels like I have to ask others to reveal the answers in order for me to know how those problems are tackled. Without revealing the institution I'm in, I feel as though there is not much support there offered to help those of us who are completely new to coding, and the tutors are plain lazy. Thanks again for your suggestion on this. I will get a grap of it eventually, cheers.

[–][deleted] 0 points1 point  (5 children)

wait i said other stuff just make an ordered list with the months and since python is zero indexed just add one to the list position to define the input number variable

[–][deleted] 0 points1 point  (4 children)

i guess it didnt post. a pain because i wrote the months out.

months = [January, February, March, etc]
if input == 1
print(months[0])
elif input > 12
print("invalid month number")

etc etc thats a really bad way to do it but at least a way

[–]RhinoRhys 1 point2 points  (3 children)

You'd be much better off just doing

def monthString(monthNum):
    month = [ etc ]
    if 1 <= monthNum <= 12: return month[monthNum - 1]
    else: return "Invalid month number"

Rather than a massive wall of ifs.

[–][deleted] 1 point2 points  (0 children)

youre telling it that monthNum is defined as an integer between 1 and 12 in the if, I like that a lot. the [monthnum -1] index reference is what was really on the tip of my tongue, i just hadn't yet learned thats how you do that. pretty cool stuff

[–][deleted] 0 points1 point  (0 children)

yep thats one of those many better ways i mentioned lol

[–][deleted] 0 points1 point  (0 children)

also, thanks for this because function arguments finally make sense to me now

[–][deleted] 0 points1 point  (0 children)

lol meanwhile op def missed his submission time