Use the globals() Python dictionary.

E.g.,

$ python
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> globals()['q' + str(5)] = 15
>>> q5
15


On Sat, Dec 1, 2018 at 3:36 PM Tevian Dray <dr...@oregonstate.edu> wrote:
>
> How can I create variable names programmatically, then assign values to them?
>
> I've tried creating variables with something like
>     var('q'+str(n))
> but my attempts to assign a value to such a variable have been unsuccessful.
> The underlying problem is that I do not know in advance how many variables 
> will
> be required, so I need some way to assign values to indexed variables in some 
> sort
> of loop based on the index.  But I can't figure out how to simultaneously 
> call the variable
> using a parameter, and assign a value to this parametrized variable.
>
> This may be a Python issue, rather than Sage...
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sage-support+unsubscr...@googlegroups.com.
> To post to this group, send email to sage-support@googlegroups.com.
> Visit this group at https://groups.google.com/group/sage-support.
> For more options, visit https://groups.google.com/d/optout.



-- 
William (http://wstein.org)

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Reply via email to