Ask Your Question

How to cast a string with an exponent from sys.argv to an Integer ?

asked 2015-09-26 17:00:01 +0100

Onox gravatar image


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

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-09-26 17:09:32 +0100

calc314 gravatar image

I believe sage_eval should take care of this issue. For example, the code below produces an integer.

s = '123^1234'
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-09-26 17:00:01 +0100

Seen: 643 times

Last updated: Sep 26 '15