Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hi! I think there were some editing difficulties.. I don't see any parentheses in your code, or quotation marks (are John and Cathy global variables?) and the SyntaxError suggests something was mistyped, so knowing exactly what you entered might be important. Your question is also more Python-related than Sage-related, so it'd probably be more useful to spend time with a python tutorial or on one of the python support groups than here for these kinds of questions. But while we're here.. :^)

There are two easy ways you can load values into a dictionary in a function and use them later. You could build the dictionary in the function and then return it using the return statement:

def put():
    u = {}
    u['Cathy'] = 3232556
    u['John'] = 4256342
    return u

v = put()
print v['Cathy']

or you can build the dictionary outside, call the function with the dictionary as an argument, and add the keys inside:

def put2(u):
    u['Cathy'] = 3232556
    u['John'] = 4256342

u = {}
put2(u)
print u['Cathy']