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?
TroudeloupAsked:
Who is Participating?
 
peprConnect With a Mentor Commented:
For the part "how do I clear the output window of everything between different outputs?"
I assume you mean the console window like the cmd window in MS Windows or terminal window in UNIX-like systems. The "clear screen" shell command may differ. Here the example shows using 'cls' command for the Windows cmd window:

import os

print 'aaaaaaaaaaaaaaaa'
print 'aaaaaaaaaaaaaaaa'
print 'aaaaaaaaaaaaaaaa'
print 'aaaaaaaaaaaaaaaa'
print 'aaaaaaaaaaaaaaaa'
print 'aaaaaaaaaaaaaaaa'
print 'aaaaaaaaaaaaaaaa'
print 'aaaaaaaaaaaaaaaa'

n = raw_input('waiting for ENTER key')

os.system('cls')                 # this is the kernel of the answer

print 'bbbbbbbbbbbbbbbbbbbb'
print 'bbbbbbbbbbbbbbbbbbbb'
print 'bbbbbbbbbbbbbbbbbbbb'
print 'bbbbbbbbbbbbbbbbbbbb'
print 'bbbbbbbbbbbbbbbbbbbb'
print 'bbbbbbbbbbbbbbbbbbbb'
print 'bbbbbbbbbbbbbbbbbbbb'
print 'bbbbbbbbbbbbbbbbbbbb'
print 'bbbbbbbbbbbbbbbbbbbb'
print 'bbbbbbbbbbbbbbbbbbbb'
print 'bbbbbbbbbbbbbbbbbbbb'

n = raw_input('waiting for ENTER key')



Note: From Python version 2.4, using the subprocess module should be prefered to os.system(). See the doc of the subprocess module.
0
 
ghostdog74Commented:
you take input with raw_input() : http://docs.python.org/lib/built-in-funcs.html
you do random generators with random module : http://docs.python.org/lib/module-random.html
0
 
peprCommented:
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)


0
 
peprCommented:
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')
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.