Well...I think I have resolved that problem. I am still not totally clear
on when Sage references Sage variables vs. Python variables.
Nevertheless...heh...I'm sure I will find out.

But, I would like to restate what I was trying to achieve. 

Let's say I have a need to display a Question and an Answer.

The source for the Question and Answer  comes from a single string. 

And suppose  the string has 0 -> n sub-strings that are to be replaced with 
random strings. The substituted Question string is to be
displayed, and then evaluated to derive an Answer string.

Here is my working example (after 10 attempts) to create the Question= 
"2*random"
and the Answer=2*random.


*exec(compile("r=random_prime(100);Q='2*'+str(r);A=2*r 
","Users/user6/TEMP.TXT","single"))  *

Then Question can be set to Q
and Answer to A.

So, is there a better way to do this?  I intend to perform
this task within a Python program running under SageMath.

Thank you. 



On Saturday, June 19, 2021 at 6:18:50 PM UTC-7 enthor wrote:

> My objective is to concisely define an expression with variables,
> assign values to the variables, and then compute the answer.
>
> Here is what I did today:
>
> ┌────────────────────────────────────────────────────────────────────┐
> │ SageMath version 9.3.rc4, Release Date: 2021-04-18                 │
> │ Using Python 3.9.2. Type "help()" for help.                        │
> └────────────────────────────────────────────────────────────────────┘
> ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
> ┃ Warning: this is a prerelease version, and it may be unstable.     ┃
> ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
> sage: z = SR.var('z')                                                     
>                                               
> sage: i = 2 * z                                                           
>                                               
> sage: i                                                                   
>                                               
> 2*z
> sage: z                                                                   
>                                               
> z
> sage: type(i)                                                             
>                                               
> <class 'sage.symbolic.expression.Expression'>
> sage: type(z)                                                             
>                                               
> <class 'sage.symbolic.expression.Expression'>
> sage: z = random_prime(100)                                               
>                                               
> sage: z                                                                   
>                                               
> 79
> sage: type(z)                                                             
>                                               
> <class 'sage.rings.integer.Integer'>
> sage: i                                                                   
>                                               
> 2*z
> sage: valuation(i)                                                         
>                                              
> ---------------------------------------------------------------------------
> AttributeError                            Traceback (most recent call last)
> /var/tmp/sage-9.3.rc4-current/local/lib/python3.9/site-packages/sage/arith/misc.py
>  
> in valuation(m, *args, **kwds)
>     743     try:
> --> 744         return m.valuation(*args, **kwds)
>     745     except AttributeError:
>
> /var/tmp/sage-9.3.rc4-current/local/lib/python3.9/site-packages/sage/structure/element.pyx
>  
> in sage.structure.element.Element.__getattr__ 
> (build/cythonized/sage/structure/element.c:4709)()
>     492         """
> --> 493         return self.getattr_from_category(name)
>     494 
>
> /var/tmp/sage-9.3.rc4-current/local/lib/python3.9/site-packages/sage/structure/element.pyx
>  
> in sage.structure.element.Element.getattr_from_category 
> (build/cythonized/sage/structure/element.c:4821)()
>     505             cls = P._abstract_element_class
> --> 506         return getattr_from_other_class(self, cls, name)
>     507 
>
> /var/tmp/sage-9.3.rc4-current/local/lib/python3.9/site-packages/sage/cpython/getattr.pyx
>  
> in sage.cpython.getattr.getattr_from_other_class 
> (build/cythonized/sage/cpython/getattr.c:2619)()
>     371         dummy_error_message.name = name
> --> 372         raise AttributeError(dummy_error_message)
>     373     attribute = <object>attr
>
> AttributeError: 'sage.symbolic.expression.Expression' object has no 
> attribute '__custom_name'
>
> During handling of the above exception, another exception occurred:
>
> TypeError                                 Traceback (most recent call last)
> /var/tmp/sage-9.3.rc4-current/local/lib/python3.9/site-packages/sage/symbolic/expression.pyx
>  
> in sage.symbolic.expression.Expression._integer_ 
> (build/cythonized/sage/symbolic/expression.cpp:8808)()
>    1298         try:
> -> 1299             n = self.pyobject()
>    1300         except TypeError:
>
> /var/tmp/sage-9.3.rc4-current/local/lib/python3.9/site-packages/sage/symbolic/expression.pyx
>  
> in sage.symbolic.expression.Expression.pyobject 
> (build/cythonized/sage/symbolic/expression.cpp:6143)()
>     586         if not is_a_numeric(self._gobj):
> --> 587             raise TypeError("self must be a numeric expression")
>     588         return py_object_from_numeric(self._gobj)
>
> TypeError: self must be a numeric expression
>
> During handling of the above exception, another exception occurred:
>
> TypeError                                 Traceback (most recent call last)
> <ipython-input-11-227398a9d1bf> in <module>
> ----> 1 valuation(i)
>
> /var/tmp/sage-9.3.rc4-current/local/lib/python3.9/site-packages/sage/arith/misc.py
>  
> in valuation(m, *args, **kwds)
>     744         return m.valuation(*args, **kwds)
>     745     except AttributeError:
> --> 746         return ZZ(m).valuation(*args, **kwds)
>     747 
>     748 
>
> /var/tmp/sage-9.3.rc4-current/local/lib/python3.9/site-packages/sage/structure/parent.pyx
>  
> in sage.structure.parent.Parent.__call__ 
> (build/cythonized/sage/structure/parent.c:9335)()
>     896         if mor is not None:
>     897             if no_extra_args:
> --> 898                 return mor._call_(x)
>     899             else:
>     900                 return mor._call_with_args(x, args, kwds)
>
> /var/tmp/sage-9.3.rc4-current/local/lib/python3.9/site-packages/sage/structure/coerce_maps.pyx
>  
> in sage.structure.coerce_maps.NamedConvertMap._call_ 
> (build/cythonized/sage/structure/coerce_maps.c:6042)()
>     285             raise TypeError("Cannot coerce {} to {}".format(x, C))
>     286         cdef Map m
> --> 287         cdef Element e = method(C)
>     288         if e is None:
>     289             raise RuntimeError("BUG in coercion model: {} method 
> of {} returned None".format(self.method_name, type(x)))
>
> /var/tmp/sage-9.3.rc4-current/local/lib/python3.9/site-packages/sage/symbolic/expression.pyx
>  
> in sage.symbolic.expression.Expression._integer_ 
> (build/cythonized/sage/symbolic/expression.cpp:8857)()
>    1299             n = self.pyobject()
>    1300         except TypeError:
> -> 1301             raise TypeError("unable to convert %r to an integer" % 
> self)
>    1302         if isinstance(n, sage.rings.integer.Integer):
>    1303             return n
>
> TypeError: unable to convert 2*z to an integer
> 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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-support/cc2bcff0-7261-41cb-87b0-22b81a89c770n%40googlegroups.com.

Reply via email to