Python Figuring out "Definition" in Python 2.7 Anaconda Birthday Dictionary

AI Thread Summary
The discussion revolves around using a dictionary in Python 2.7 to retrieve birthdates associated with names. The user has a dictionary called 'Birth' where names are keys and their corresponding birthdates are values. They seek clarification on how to print a specific name's birthdate using the input from the user. It is emphasized that the correct way to access a birthdate is through the syntax `Birth[name]`, where `name` is the variable holding the user's input. There is confusion about whether `Birth[name]` returns the name or the birthdate, with participants clarifying that it returns the value (birthdate) associated with the key (name). The conversation also touches on potential issues with key formatting and the importance of standardizing names for better dictionary access. Ultimately, the user indicates they have resolved their issue but finds it challenging to articulate the problem.
WWGD
Science Advisor
Homework Helper
Messages
7,678
Reaction score
12,360
TL;DR Summary
I have a dictionary ' Birth' on birthdates: list of names and respective birthday pairs, e.g., 'Bob':'April1'
I can call a name in the list through Birth[name]. How do I call Birth[name]'s date?
--- Say we have the dictionary in Anaconda 2.7 Jupyter :---
B={'A1':'Jan1' , 'A2':'Feb2',...} of Birthdates.
---I can call a name , e.g., 'A1' , using Birth[name]. As in :---
print('What is your name '\? ')
name=input()

print('Yes, Birth[name] is in our list')
---How do I call a Date? I want to print out a given name's birthdate,---
---Something like :---
print(' A1's birthday is on ...')
 
  • Like
Likes pbuk
Technology news on Phys.org
jedishrfu said:
Yes, but you might have to remove any trailing carriage return characters from the name variable once you’ve read it in.

https://www.w3schools.com/python/python_dictionaries.asp
Yes, I figure I must somehow connect the name with the call so that it can be tied to the birthdate. Just can't think of how to do it. Maybe @StoneTemplePython knows?
 
Last edited:
That does not make much sense, unless 3.7 is much different then 2.7.
WWGD said:
I can call a name in the list through Birth[name]. How do I call Birth[name]'s date?
In 3.7 what you do is actually gives not the key vut the value.

dict_name[key]=value

Also the thing that you are describing does not make much sense.

You are saying that
dict_name[key] = [key] ??
 
No, I want the value for the keys in my dictionary Birth having a list of names as the keys and birthdates as the associated values. Birth[name] gives me the key , i.e., the name but I don't know what string to use to find the value , i.e.. , birthdate so that Birth[string] spits out the birthdate of a specific name. I will post the code tomorrow. Clearly, 'string' in above must somehow point to the name, just not sure of how.
 
WWGD said:
Summary: I have a dictionary ' Birth' on birthdates: list of names and respective birthday pairs, e.g., 'Bob':'April1'
I can call a name in the list through Birth[name]. How do I call Birth[name]'s date?

I can call a name , e.g., 'A1' , using Birth[name]. As in :---
print('What is your name '\? ')
name=input()

print('Yes, Birth[name] is in our list')

What do you think print(Birth[name]) will print? You are calling it a "name", but it isn't, it's the birthdate corresponding to the name. If the name variable holds the value 'Bob', then Birth[name] returns the value 'April1'. That's not a name.

I don't understand what you mean by "call Birth[name]'s date". If you want to retrieve the birthdate corresponding to a name, you are already doing that: that's what Birth[name] does, returns the value, such as 'April1', stored in the dictionary under the key stored in the name variable, such as 'Bob'. If you want to do something else, then you need to explain what.
 
WWGD said:
Birth[name] gives me the key , i.e., the name

No, it doesn't. It gives you the value corresponding to the key stored in the name variable.
 
WWGD said:
I don't know what string to use to find the value , i.e.. , birthdate so that Birth[string] spits out the birthdate of a specific name

The string is the name itself. If you want to find Bob's birthdate, it's Birth['Bob']. Or you store the value 'Bob' in the name variable, and then the birthdate is retrieved by Birth[name].
 
WWGD said:
Yes, I figure I must somehow connect the name with the call so that it can be tied to the birthdate. Just can't think of how to do it. Maybe @StoneTemplePython knows?

weird, I just wandered into this thread and didn't get any alert here. Maybe another PF5 bug?
WWGD said:
print('Yes, Birth[name] is in our list')
---How do I call a Date? I want to print out a given name's birthdate,---
---Something like :---
print(' A1's birthday is on ...')
I think Peter has this, and I should point out that I've been slowly purging my knowledge of Python 2.x.

On the other hand I find it immensely confusing to have a thread titled "... Python 2.7..."

and then see a print function like print(' A1's birthday is on ...'), with no 'from future import ___" listed
 
  • #10
The name variable is the key to the dictionary. In general its a very poor key to use. Youll have to standardize it to all caps and perhaps remove any spaces to make it a better key.

Its not uncommon in python code to have the dictionary return a tuple of values associated with the key like address and birthday and age depending on how you plan to use it.

I built something similar using file extension as a key and returning a tuple of formatting parameters and keywords for different programming languages for colorizing my code using html tags.
 
  • #11
Never mind, thanks all, I figured it out. Hard to explain.
 
Back
Top