Ask Your Question

getting user imput in Python scripts

asked 2013-02-25 21:09:24 -0500

nerak99 gravatar image

updated 2013-02-26 09:49:32 -0500

How can I write a script that will get user input when the script runs. I am talking about the sage notebook() here rather than the cli sage.

edit retag flag offensive close merge delete

1 answer

Sort by ┬╗ oldest newest most voted

answered 2013-02-25 21:58:36 -0500

ppurka gravatar image

This is not really a Sage question. Anyway, use input in python.

In [1]: a = input('Enter number: ')
Enter number: 10

In [2]: a
Out[2]: 10

In [3]: a = input('Enter string')
Enter string: 'test'

In [4]: a
Out[4]: 'test'

In the sage notebook you can use the @interact decorator. See examples of it here.

edit flag offensive delete link more


The OP used `guess = int(raw_input('Take a guess: '))` (unsuccessfully) in his Stackoverflow question - would this work similarly? In particular, he's talking about using it in a *script*, not interactively (as far as I can tell). (

kcrisman gravatar imagekcrisman ( 2013-02-26 02:19:44 -0500 )edit

The `input` command works in scripts too. ~/tmp/Downloadz┬╗ sage -python num: 2 2 ~/tmp/Downloadz┬╗ cat a = input('num: ') print a If the OP wants to use it in the notebook, then (s)he should use the interact decorator.

ppurka gravatar imageppurka ( 2013-02-26 14:59:00 -0500 )edit

Yup, I see what you mean now - I didn't read it carefully, the original post at SO did not mention the notebook.

kcrisman gravatar imagekcrisman ( 2013-02-26 16:40:59 -0500 )edit

Absolutely I didn't, sorry about that. I am using sage as a way of allowing children in class to do some Python coding in a situation where the service provider won't allow installation of any programming software.

nerak99 gravatar imagenerak99 ( 2013-02-26 22:23:23 -0500 )edit

Cool! We love this - what age group, if you don't mind us asking? (This info can be helpful for obtaining grants, for instance...)

kcrisman gravatar imagekcrisman ( 2013-02-27 03:33:47 -0500 )edit

Your Answer

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

Add Answer

Question Tools


Asked: 2013-02-25 21:09:24 -0500

Seen: 540 times

Last updated: Feb 26 '13