getting user imput in Python scripts
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.
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.
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)
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.
Yup, I see what you mean now - I didn't read it carefully, the original post at SO did not mention the notebook.
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.
Cool! We love this - what age group, if you don't mind us asking? (This info can be helpful for obtaining grants, for instance...)
Please start posting anonymously - your entry will be published after you log in or create a new account.
Asked: 12 years ago
Seen: 2,308 times
Last updated: Feb 26 '13