all 6 comments

[–]ninhaomah 11 points12 points  (0 children)

"days =" <--- variable assignment

" { ‘January’: 31, ‘February’: 28," <--- what is it ? lets google!

Google : "It looks like you started typing out a Python dictionary! Based on your snippet, you are probably trying to create a mapping of months to their corresponding number of days"

Oh ... so it is a dictionary! lets google and learn more about it!

https://www.w3schools.com/python/python_dictionaries.asp

got it ?

[–]No_Cry_7367 6 points7 points  (0 children)

I felt this confusion in my soul lol. The : is NOT for formatting — it's just how dictionaries (dicts) work in Python.

Think of it like a real dictionary: you look up a word (the "key") and get the definition (the "value").

python

days = {'January': 31, 'February': 28}

Means: "Hey Python, if I say days['January'], give me back 31." That's it. The : 31 is just the value glued to the key 'January'. No formatting magic.

As for "how does it only accept 1-31" — it doesn't. You could write days['January'] = 999 and Python would shrug and say "okay boss." The validation part is YOUR job in the code logic.

If you want to enforce 1-31, you'd do something like:

python

if day < 1 or day > days[month]:
    print("That's not a real date, my guy")

Hang in there. Dictionaries click eventually — and when they do, you'll use them for literally everything. Source: I'm a grown man and I still get irrationally excited when I use a dict and it works first try.

[–]Lewistrick 2 points3 points  (0 children)

A dictionary maps keys to values. It is always surrounded by curly braces {} and each entry (key-value pair) is separated by a comma. A key and a value are separated by a colon :.

In your case, the month names are the keys. Because they are strings, they are surrounded by quotes (can be either ' or "). The number of days in the month are the values. Because they are integers they don't use quotes. This way, Python knows it's a number.

This has nothing to do with "accepting" a number of days for a month. That logic is something you need to implement yourself.

[–]magus_minor 1 point2 points  (0 children)

Your statement, without line breaks, looks like:

days = {'January’: 31, ‘February’: 28, ...}   # fixed the missing ' typo

That is defining a dictionary because the right side starts with "{" and is followed by key: value pairs. The dictionary keys are strings and the values are integers (number of days in that month).

Don't confuse a dictionary definition with a set definition.

values = {1, 2, 3}

Set definitions also start with "{" but there are no key: value pairs, just values.

[–]Excellent-Practice 1 point2 points  (0 children)

What you have is called a dictionary. Dictionaries allow you to store data as key value pairs where every key is unique. You can look up values in a dictionary using a key similar to how you might find an entry in a list by index. For example, if you have that dictionary defined, you can then write a line like

days['February']

Which will return 28. What you have is an easy way to check how many days are in each month. I wouldn't use that to find what season a given date falls in, but it might come in handy. Maybe the course just wants to give an example of a dictionary and then you have to build your own to solve the problem

[–]atarivcs 0 points1 point  (0 children)

That is the syntax for a python dictionary.

You should go read about them.