Ask Your Question
0

A programing question

asked 2011-01-05 09:41:53 +0100

updated 2011-01-05 14:41:31 +0100

Hello!

I would like to define a procedure, which puts values in a dictionary:

def put:
    u={}
    u[Cathy]=3232556
    u[John]=4256342

Now if I am outside the procedure, I cannot use the values of u's... Is there a method for that outside the procedura I can use these specific u's?

def put:
    u={}
    u[Cathy]=3232556
    u[John]=4256342

put

print u[Cathy]

Traceback (click to the left of this block for traceback)
...
SyntaxError: invalid syntax

Many Thanks!

edit retag flag offensive close merge delete

Comments

Note that it should be "def put()", and the function call should be "put()". You're missing parentheses in both places.

John Palmieri gravatar imageJohn Palmieri ( 2011-01-05 16:40:03 +0100 )edit

3 Answers

Sort by ยป oldest newest most voted
1

answered 2011-01-05 10:33:18 +0100

You can do something like this:

def put(): 
   global u   # this makes 'u' available outside of this function
   u={}
   u['Cathy']=3232556
   u['John']=4256342

sage: put()
sage: u
{'Cathy': 3232556, 'John': 4256342}
sage: u['Cathy'] = 12
sage: u
{'Cathy': 12, 'John': 4256342}
edit flag offensive delete link more

Comments

Thank you! It will help me :-)

Katika gravatar imageKatika ( 2011-01-05 13:43:31 +0100 )edit

generally it is better to avoid global variables, especially for the programs longer than a page. In this example it would make more sense to return the "u" from the "put". More descriptive variable names are also helpful.

Evgeny gravatar imageEvgeny ( 2011-01-05 16:46:51 +0100 )edit

When I need to return with two variables (I use two dictionaries u and v, and I need both later), is there a method which returns with both u and v?

Katika gravatar imageKatika ( 2011-01-05 17:25:35 +0100 )edit

Yes, in python in is possible to return more than one value: "return a, b" then in the calling code you can have "a, b = put()"

Evgeny gravatar imageEvgeny ( 2011-01-05 17:38:01 +0100 )edit
1

answered 2011-01-05 10:36:53 +0100

DSM gravatar image

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']
edit flag offensive delete link more

Comments

Hello! I was in a trouble editing my question, i don't know how to put indentations... Indeed, I write an indentation and it doesn't appear... I am a beginner both in Sage and in programming, and to read python that's something very hard for me :-) But this forum helps me a lot :-)

Katika gravatar imageKatika ( 2011-01-05 13:42:36 +0100 )edit

To get a code block to display correctly, you need to indent it at least 4 spaces. I've edited your question to fix that.

John Palmieri gravatar imageJohn Palmieri ( 2011-01-05 16:41:27 +0100 )edit

Thanks! So my next question will have a better form :-)

Katika gravatar imageKatika ( 2011-01-05 17:30:58 +0100 )edit
0

answered 2011-01-06 11:50:25 +0100

niles gravatar image

To clarify some of the comments: here is an example using "return", and returning a tuple of items rather than a single one: You tell the function to return the internal values u and v, and then when you call the function, you can store the output in any variable you want (a and b, in this case).

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

   v={}
   v['Bill']=7639867
   v['Amy']=3528520
   return (u,v)

sage: a,b = put()
sage: a
{'Cathy': 3232556, 'John': 4256342}
sage: b
{'Amy': 3528520, 'Bill': 7639867}
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-01-05 09:41:53 +0100

Seen: 713 times

Last updated: Jan 06 '11