Figuring out "Definition" in Python 2.7 Anaconda Birthday Dictionary

In summary, you are trying to use the name variable as a key to a dictionary, but it is not a good key to use because it can hold different values for different names. You might be better off using a different variable to store the birthdates, or storing them as tuples.
  • #1
WWGD
Science Advisor
Gold Member
7,084
10,725
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
  • #3
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:
  • #4
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] ??
 
  • #5
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.
 
  • #6
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.
 
  • #7
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.
 
  • #8
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].
 
  • #9
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.
 

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

1. What is the purpose of "Definition" in Python 2.7 Anaconda Birthday Dictionary?

The "Definition" in Python 2.7 Anaconda Birthday Dictionary is used to store and retrieve information about a person's birthday. It allows the user to input a name and their corresponding birthday, and then retrieve that information in the future.

2. How do I add a new entry to the Birthday Dictionary?

To add a new entry to the Birthday Dictionary, you can use the "input" function in Python to prompt the user for the person's name and birthday. Then, you can use the "dictionary_name[new_key] = new_value" code to add the new entry to the dictionary. Make sure to save the changes to the dictionary for future use.

3. Can I edit or update an existing entry in the Birthday Dictionary?

Yes, you can edit or update an existing entry in the Birthday Dictionary. You can use the same code as adding a new entry, but instead of using "new_key," use the existing person's name that you want to update. This will overwrite the previous value with the new one.

4. How do I retrieve a person's birthday from the Birthday Dictionary?

To retrieve a person's birthday from the Birthday Dictionary, you can use the "get" function in Python and specify the person's name as the key. This will return the corresponding birthday value for that person, if it exists in the dictionary.

5. What if I want to delete an entry from the Birthday Dictionary?

If you want to delete an entry from the Birthday Dictionary, you can use the "del" statement in Python and specify the key (person's name) that you want to delete. This will remove the entire entry from the dictionary, permanently deleting it from the data structure.

Similar threads

  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
3
Views
443
  • Programming and Computer Science
Replies
2
Views
909
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
7K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
3K
Replies
41
Views
7K
  • Special and General Relativity
Replies
1
Views
2K
Back
Top