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.Sat, 26 Sep 2015 17:09:32 +0200How to cast a string with an exponent from sys.argv to an Integer ?https://ask.sagemath.org/question/29597/how-to-cast-a-string-with-an-exponent-from-sysargv-to-an-integer/Hi,
For an exercise, I have written a Python script that takes an integer argument. Of course, the input in sys.argv is of type 'str', so I cast it to an Integer. It works, but I am supposed to accept numbers written like '123^1237' too, with a hat for the exponent. And I cannot find how to do that. In the sage interpreter, I can type "n = Integer(123^1237)" without any problem, but in my script it fails with a "TypeError: unable to convert '123^1237' to an integer". ThanksSat, 26 Sep 2015 17:00:01 +0200https://ask.sagemath.org/question/29597/how-to-cast-a-string-with-an-exponent-from-sysargv-to-an-integer/Answer by calc314 for <p>Hi,</p>
<p>For an exercise, I have written a Python script that takes an integer argument. Of course, the input in sys.argv is of type 'str', so I cast it to an Integer. It works, but I am supposed to accept numbers written like '123^1237' too, with a hat for the exponent. And I cannot find how to do that. In the sage interpreter, I can type "n = Integer(123^1237)" without any problem, but in my script it fails with a "TypeError: unable to convert '123^1237' to an integer". Thanks</p>
https://ask.sagemath.org/question/29597/how-to-cast-a-string-with-an-exponent-from-sysargv-to-an-integer/?answer=29598#post-id-29598I believe `sage_eval` should take care of this issue. For example, the code below produces an integer.
s = '123^1234'
sage_eval(s)
Sat, 26 Sep 2015 17:09:32 +0200https://ask.sagemath.org/question/29597/how-to-cast-a-string-with-an-exponent-from-sysargv-to-an-integer/?answer=29598#post-id-29598