Troudeloup
asked on
python keyboard input
#include <iostream>
using namespace std;
int main()
{
int a;
cout << "please input a number" << endl;
cin >> a;
cout << "this is the number " << a << endl;
}
I am trying to write the same thing in python,
how do I take input with python?
how do I do a random number generator that generate only interger between n and m ?
( or rather, I image there is one simple to use function to do all of those)
how do I clear the output window of everything between different outputs?
using namespace std;
int main()
{
int a;
cout << "please input a number" << endl;
cin >> a;
cout << "this is the number " << a << endl;
}
I am trying to write the same thing in python,
how do I take input with python?
how do I do a random number generator that generate only interger between n and m ?
( or rather, I image there is one simple to use function to do all of those)
how do I clear the output window of everything between different outputs?
For the first part:
s = raw_input('please enter a number ')
a = int(s)
print 'this is a number', a
Use always raw_input() and convert the read string to the int explicitly. There is a input() function that would convert it automatically; however, it is dangerous. A malicious user could type here a dangerous Python code that would be executed.
For the random, try this:
import random
m = 1
n = 6
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
s = raw_input('please enter a number ')
a = int(s)
print 'this is a number', a
Use always raw_input() and convert the read string to the int explicitly. There is a input() function that would convert it automatically; however, it is dangerous. A malicious user could type here a dangerous Python code that would be executed.
For the random, try this:
import random
m = 1
n = 6
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
print random.randint(m, n)
One more note to printing and to std::cout. If you want to output the text using the print statement, then or you have to consider the automatic \n appended by the print or you have to add a trailing comma to suppress the extra \n.
If you want to have full control over the produced output, I would recommend to use sys.stdout.write() instead. However, the print is good for quickly adding some output to the script.
With imported sys module, you can also distinguish between the stdout and stderr. You can do the same also with print; however, the syntax became less readable and more complicated. The effect of easiness of the print statement disappears ;) Try this:
import sys
print 'Text sent to stdout via print.'
print >> sys.stderr, 'Text sent to stderr via print.'
sys.stdout.write('Text sent to stdout via sys.\n')
sys.stderr.write('Text sent to stderr via sys.\n')
If you want to have full control over the produced output, I would recommend to use sys.stdout.write() instead. However, the print is good for quickly adding some output to the script.
With imported sys module, you can also distinguish between the stdout and stderr. You can do the same also with print; however, the syntax became less readable and more complicated. The effect of easiness of the print statement disappears ;) Try this:
import sys
print 'Text sent to stdout via print.'
print >> sys.stderr, 'Text sent to stderr via print.'
sys.stdout.write('Text sent to stdout via sys.\n')
sys.stderr.write('Text sent to stderr via sys.\n')
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you do random generators with random module : http://docs.python.org/lib/module-random.html