Ask Your Question
0

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

asked 9 years ago

Onox gravatar image

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". Thanks

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
0

answered 9 years ago

calc314 gravatar image

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

s = '123^1234'
sage_eval(s)
Preview: (hide)
link

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

Stats

Asked: 9 years ago

Seen: 767 times

Last updated: Sep 26 '15