Ask Your Question
1

getting user imput in Python scripts

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

nerak99 gravatar image

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

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
3

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

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

Comments

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). (http://stackoverflow.com/questions/15085251/getting-user-input-in-sage-python)

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

The `input` command works in scripts too. ~/tmp/Downloadz┬╗ sage -python a.py num: 2 2 ~/tmp/Downloadz┬╗ cat a.py 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 -0600 )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 -0600 )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 -0600 )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 -0600 )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

Stats

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

Seen: 478 times

Last updated: Feb 26 '13