Ask Your Question
0

A programing question

asked 14 years ago

updated 14 years ago

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!

Preview: (hide)

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 ( 14 years ago )

3 Answers

Sort by » oldest newest most voted
1

answered 14 years ago

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}
Preview: (hide)
link

Comments

Thank you! It will help me :-)

Katika gravatar imageKatika ( 14 years ago )

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 ( 14 years ago )

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 ( 14 years ago )

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 ( 14 years ago )
1

answered 14 years ago

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']
Preview: (hide)
link

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 ( 14 years ago )

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 ( 14 years ago )

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

Katika gravatar imageKatika ( 14 years ago )
0

answered 14 years ago

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}
Preview: (hide)
link

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: 14 years ago

Seen: 792 times

Last updated: Jan 06 '11