ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 31 Aug 2016 22:16:50 +0200what is the equivalent of cin>> in sagehttps://ask.sagemath.org/question/34671/what-is-the-equivalent-of-cin-in-sage/I am familiar with C++. In C++, the syntax to give input is cin>>. What is the equivalent of cin>> in sage ?
I am declaring an integer $n$ (in C++ syntax we write int n) and the user will give the value of $n$ when the program is being run.
How do I do that ?
It would be really helpful if you can give an example. (Say, the purpose of the program is to print an integer given by the user. What will be the syntax ?)Wed, 31 Aug 2016 22:08:43 +0200https://ask.sagemath.org/question/34671/what-is-the-equivalent-of-cin-in-sage/Answer by kcrisman for <p>I am familiar with C++. In C++, the syntax to give input is cin>>. What is the equivalent of cin>> in sage ?
I am declaring an integer $n$ (in C++ syntax we write int n) and the user will give the value of $n$ when the program is being run.</p>
<p>How do I do that ?</p>
<p>It would be really helpful if you can give an example. (Say, the purpose of the program is to print an integer given by the user. What will be the syntax ?)</p>
https://ask.sagemath.org/question/34671/what-is-the-equivalent-of-cin-in-sage/?answer=34672#post-id-34672I *guess* you could use `raw_input()/input()` ([depending on Py2 versus Py3](http://stackoverflow.com/questions/954834/how-do-i-use-raw-input-in-python-3)). If I understand your question correctly?
BUT you will have to be careful, because the raw input won't be preparsed (although you can ask for it to do that too, I guess). E.g. a file `test.sage` like this is bad:
a = raw_input()
print a^2
while
a = raw_input()
print Integer(a)^2
does more what one would expect.Wed, 31 Aug 2016 22:16:50 +0200https://ask.sagemath.org/question/34671/what-is-the-equivalent-of-cin-in-sage/?answer=34672#post-id-34672