ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 06 Jan 2011 11:50:25 +0100A programing questionhttps://ask.sagemath.org/question/7840/a-programing-question/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!Wed, 05 Jan 2011 09:41:53 +0100https://ask.sagemath.org/question/7840/a-programing-question/Comment by John Palmieri for <p>Hello!</p>
<p>I would like to define a procedure, which puts values in a dictionary:</p>
<pre><code>def put:
u={}
u[Cathy]=3232556
u[John]=4256342
</code></pre>
<p>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?</p>
<pre><code>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
</code></pre>
<p>Many Thanks!</p>
https://ask.sagemath.org/question/7840/a-programing-question/?comment=22333#post-id-22333Note that it should be "def put()", and the function call should be "put()". You're missing parentheses in both places. Wed, 05 Jan 2011 16:40:03 +0100https://ask.sagemath.org/question/7840/a-programing-question/?comment=22333#post-id-22333Answer by niles for <p>Hello!</p>
<p>I would like to define a procedure, which puts values in a dictionary:</p>
<pre><code>def put:
u={}
u[Cathy]=3232556
u[John]=4256342
</code></pre>
<p>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?</p>
<pre><code>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
</code></pre>
<p>Many Thanks!</p>
https://ask.sagemath.org/question/7840/a-programing-question/?answer=11935#post-id-11935To 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}Thu, 06 Jan 2011 11:50:25 +0100https://ask.sagemath.org/question/7840/a-programing-question/?answer=11935#post-id-11935Answer by John Palmieri for <p>Hello!</p>
<p>I would like to define a procedure, which puts values in a dictionary:</p>
<pre><code>def put:
u={}
u[Cathy]=3232556
u[John]=4256342
</code></pre>
<p>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?</p>
<pre><code>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
</code></pre>
<p>Many Thanks!</p>
https://ask.sagemath.org/question/7840/a-programing-question/?answer=11924#post-id-11924You 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}
Wed, 05 Jan 2011 10:33:18 +0100https://ask.sagemath.org/question/7840/a-programing-question/?answer=11924#post-id-11924Comment by Katika for <p>You can do something like this:</p>
<pre><code>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}
</code></pre>
https://ask.sagemath.org/question/7840/a-programing-question/?comment=22334#post-id-22334Thank you! It will help me :-)Wed, 05 Jan 2011 13:43:31 +0100https://ask.sagemath.org/question/7840/a-programing-question/?comment=22334#post-id-22334Comment by Katika for <p>You can do something like this:</p>
<pre><code>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}
</code></pre>
https://ask.sagemath.org/question/7840/a-programing-question/?comment=22330#post-id-22330When 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? Wed, 05 Jan 2011 17:25:35 +0100https://ask.sagemath.org/question/7840/a-programing-question/?comment=22330#post-id-22330Comment by Evgeny for <p>You can do something like this:</p>
<pre><code>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}
</code></pre>
https://ask.sagemath.org/question/7840/a-programing-question/?comment=22328#post-id-22328Yes, 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()"Wed, 05 Jan 2011 17:38:01 +0100https://ask.sagemath.org/question/7840/a-programing-question/?comment=22328#post-id-22328Comment by Evgeny for <p>You can do something like this:</p>
<pre><code>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}
</code></pre>
https://ask.sagemath.org/question/7840/a-programing-question/?comment=22331#post-id-22331generally 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.Wed, 05 Jan 2011 16:46:51 +0100https://ask.sagemath.org/question/7840/a-programing-question/?comment=22331#post-id-22331Answer by DSM for <p>Hello!</p>
<p>I would like to define a procedure, which puts values in a dictionary:</p>
<pre><code>def put:
u={}
u[Cathy]=3232556
u[John]=4256342
</code></pre>
<p>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?</p>
<pre><code>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
</code></pre>
<p>Many Thanks!</p>
https://ask.sagemath.org/question/7840/a-programing-question/?answer=11925#post-id-11925Hi! 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:
<pre><code>def put():
u = {}
u['Cathy'] = 3232556
u['John'] = 4256342
return u
v = put()
print v['Cathy']
</code></pre>
or you can build the dictionary outside, call the function with the dictionary as an argument, and add the keys inside:
<pre><code>def put2(u):
u['Cathy'] = 3232556
u['John'] = 4256342
u = {}
put2(u)
print u['Cathy']
</code></pre>Wed, 05 Jan 2011 10:36:53 +0100https://ask.sagemath.org/question/7840/a-programing-question/?answer=11925#post-id-11925Comment by Katika for <p>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.. :^)</p>
<p>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:</p>
<pre><code>def put():
u = {}
u['Cathy'] = 3232556
u['John'] = 4256342
return u
v = put()
print v['Cathy']
</code></pre>
<p>or you can build the dictionary outside, call the function with the dictionary as an argument, and add the keys inside:</p>
<pre><code>def put2(u):
u['Cathy'] = 3232556
u['John'] = 4256342
u = {}
put2(u)
print u['Cathy']
</code></pre>
https://ask.sagemath.org/question/7840/a-programing-question/?comment=22329#post-id-22329Thanks! So my next question will have a better form :-)Wed, 05 Jan 2011 17:30:58 +0100https://ask.sagemath.org/question/7840/a-programing-question/?comment=22329#post-id-22329Comment by John Palmieri for <p>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.. :^)</p>
<p>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:</p>
<pre><code>def put():
u = {}
u['Cathy'] = 3232556
u['John'] = 4256342
return u
v = put()
print v['Cathy']
</code></pre>
<p>or you can build the dictionary outside, call the function with the dictionary as an argument, and add the keys inside:</p>
<pre><code>def put2(u):
u['Cathy'] = 3232556
u['John'] = 4256342
u = {}
put2(u)
print u['Cathy']
</code></pre>
https://ask.sagemath.org/question/7840/a-programing-question/?comment=22332#post-id-22332To get a code block to display correctly, you need to indent it at least 4 spaces. I've edited your question to fix that.Wed, 05 Jan 2011 16:41:27 +0100https://ask.sagemath.org/question/7840/a-programing-question/?comment=22332#post-id-22332Comment by Katika for <p>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.. :^)</p>
<p>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:</p>
<pre><code>def put():
u = {}
u['Cathy'] = 3232556
u['John'] = 4256342
return u
v = put()
print v['Cathy']
</code></pre>
<p>or you can build the dictionary outside, call the function with the dictionary as an argument, and add the keys inside:</p>
<pre><code>def put2(u):
u['Cathy'] = 3232556
u['John'] = 4256342
u = {}
put2(u)
print u['Cathy']
</code></pre>
https://ask.sagemath.org/question/7840/a-programing-question/?comment=22335#post-id-22335Hello! 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 :-)Wed, 05 Jan 2011 13:42:36 +0100https://ask.sagemath.org/question/7840/a-programing-question/?comment=22335#post-id-22335