Figuring out "Definition" in Python 2.7 Anaconda Birthday Dictionary

  • Context: Python 
  • Thread starter Thread starter WWGD
  • Start date Start date
  • Tags Tags
    Definition Python
Click For Summary

Discussion Overview

The discussion revolves around how to retrieve birthdates from a dictionary in Python 2.7, specifically using a dictionary that maps names to their respective birthdates. Participants explore the syntax and logic required to access the values associated with keys in the dictionary.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes a dictionary structure where names are keys and birthdates are values, seeking clarification on how to print a birthdate using a name input.
  • Another participant suggests that the name variable may need to have trailing carriage return characters removed after input.
  • There is confusion about whether the expression Birth[name] retrieves the key or the value, with some participants asserting it retrieves the value associated with the key stored in the name variable.
  • One participant expresses uncertainty about how to connect the name input to retrieve the corresponding birthdate, indicating a need for further explanation.
  • Another participant points out that the name variable is indeed the key and suggests standardizing the names for better consistency in dictionary keys.
  • There is mention of the potential for a dictionary to return multiple values associated with a key, depending on the intended use.
  • A participant notes confusion regarding the print function syntax and the lack of a future import statement in the context of Python 2.7.
  • One participant ultimately states they figured out the solution but finds it difficult to explain.

Areas of Agreement / Disagreement

Participants express differing views on the retrieval of values from the dictionary, with some asserting that Birth[name] returns the value while others suggest it does not. The discussion remains unresolved regarding the clarity of the syntax and the best practices for using names as keys in the dictionary.

Contextual Notes

There are limitations in the discussion regarding the assumptions about input handling and the specific syntax used in Python 2.7, which may not be fully compatible with Python 3.x conventions.

WWGD
Science Advisor
Homework Helper
Messages
7,798
Reaction score
13,104
TL;DR
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   Reactions: pbuk
Technology news on Phys.org
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.
 

Similar threads

  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
395