so here is my code

from math import sqrt
class ageName:
 "class to represent a person"
def _init_(self):
    self.name = ""
    self.age = 0.0
    self.sqrtage = 0

g = ageName()
g.name = raw_input("Enter a name, or stop to exit: ")
g.age = int(raw_input("Enter an age: "))
g.sqrtage = sqrt(g.age)
list1 = dict()
list1[g.name] = g.age
print g.name, g.age, g.sqrtage
print list1

while g.name != 'stop':
    g.name = raw_input("Enter a name, or stop to exit: ")
    if g.name == 'stop':
        break
    else:
        g.age = int(raw_input("Enter an age: "))
        g.sqrtage = sqrt(g.age)
        list1.append(g.name)
        list1[g.name] = g.age
        print g.name, g.age, g.sqrtage
        print list1

the last few lines are wrong

I want to add new key-value pair to a dict

list1 = {'joe': 11, 'john', 19} for example

How do I do that? I have read the docs and google and still can't figure out a way

Member Avatar for masterofpuppets

hi,
try to simply remove line 25
You can create a new key-value pair like this:

>>> d = {}
>>> d[ "1" ] = "2"
>>> d
{'1': '2'}
>>>

like this:

from math import sqrt
class ageName:
    "class to represent a person"
    def _init_( self ):
        self.name = ""
        self.age = 0.0
        self.sqrtage = 0
 
g = ageName()
g.name = raw_input( "Enter a name, or stop to exit: " )
g.age = input( "Enter an age: " )
g.sqrtage = sqrt( g.age )
list1 = dict()
list1[ g.name ] = g.age
print g.name, g.age, g.sqrtage
print list1
 
while g.name != 'stop':
    g.name = raw_input( "Enter a name, or stop to exit: " )
    if g.name == 'stop':
        break
    else:
        g.age = input( "Enter an age: " )
        g.sqrtage = sqrt( g.age )
        list1[ g.name ] = g.age
        print g.name, g.age, g.sqrtage
        print list1

also instead of using int( raw_input() ) use input() :)

hope this helps :)

To use the AgeName class instance, with the name in the dictionary pointing to each class instance, you could do something like this.

from math import sqrt
class AgeName:
    """class to represent a person"""
    def _init_(self):
        ## 'name' is redundant since it is the key of the dictionary
        ##self.name = ""
        self.age = 0.0
        self.sqrtage = 0
 

def print_all(name_dict):
   for name in name_dict:     ## each key in the dictionary
      print name, name_dict[name].age, name_dict[name].sqrtage


name_dict = {}
name = "" 
while name != 'stop':
    name = raw_input("Enter a name, or stop to exit: ")
    if name.lower() != 'stop':
        ##  name_dict[name] now points to a class instance
        name_dict[name] = AgeName()
        name_dict[name].age = int(raw_input("Enter an age: "))
        name_dict[name].sqrtage = sqrt(name_dict[name].age)
        print name, name_dict[name].age, name_dict[name].sqrtage
print_all(name_dict)

Thank you very much master, interesting. All you did was removing that append line (which is undefined for dict).

woooee, thank you for this re-write.
I have a few questions:
1. why is name redundant?
2. what is name.lower? Where did the "lower" come from?

Thanks.

Member Avatar for masterofpuppets

Thank you very much master, interesting. All you did was removing that append line (which is undefined for dict).

woooee, thank you for this re-write.
I have a few questions:
1. why is name redundant?
2. what is name.lower? Where did the "lower" come from?

Thanks.

1. Well, as woooee mentioned, since you're using the name as the key in the dictionary, you don't need it in the class and you can easily get the person corresponding to that name like this:

person = name_dict[ "John" ]
print person.age()

2. .lower() is a method in the string class that takes a string and converts all its characters into lowercase:
e.g.

s = "This Is A TEST"
newS = s.lower()
print newS
# -> this is a test

there's also .upper() which converts to uppercase

hope this helps :)

A more normal way to use __int__ (constructor) in a python class.
And just some point that a class is storeing variables in a dictionary.

from math import sqrt

class ageName(object):
    '''class to represent a person'''
    def __init__(self, name, age):
        self.name = name
        self.age = age
        
worker1 = ageName('jon', 40)
worker2 = ageName('lise', 22)

#Now if we print this you see that variabls are stored in a dictionary
print worker1.__dict__
print worker2.__dict__
'''
{'age': 40, 'name': 'jon'}
{'age': 22, 'name': 'lise'}
'''

#when we call (key)worker1.name we are getting the (value)
print worker1.name
'''
jon
'''
print sqrt(worker2.age)
'''
4.69041575982
'''
Member Avatar for masterofpuppets

A more normal way to use __int__ (constructor) in a python class.
And just some point that a class is storeing variables in a dictionary.

from math import sqrt

class ageName(object):
    '''class to represent a person'''
    def __init__(self, name, age):
        self.name = name
        self.age = age
        
worker1 = ageName('jon', 40)
worker2 = ageName('lise', 22)

#Now if we print this you see that it`s stored in a dictionary
print worker1.__dict__
print worker2.__dict__
'''
{'age': 40, 'name': 'jon'}
{'age': 22, 'name': 'lise'}
'''

#when we call (key)worker1.name we are getting the (value)
print worker1.name
'''
jon
'''
print sqrt(worker2.age)
'''
4.69041575982
'''

ahh thanks for that dude, I didn't know about the class dictionaries :)

can't we append something to a dictionary??

can't we append something to a dictionary??

No, a python dictionary does not have an append() method. A dictionary is not an ordered data structure, unless you are using a collections.OrderedDict() instance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.