?
Solved

python    keyboard input

Posted on 2007-10-09
4
Medium Priority
?
7,414 Views
Last Modified: 2012-06-21
#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?
0
Comment
Question by:Troudeloup
  • 3
4 Comments
 
LVL 9

Expert Comment

by:ghostdog74
ID: 20046231
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
 
LVL 29

Expert Comment

by:pepr
ID: 20046614
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
 
LVL 29

Expert Comment

by:pepr
ID: 20046663
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
 
LVL 29

Accepted Solution

by:
pepr earned 2000 total points
ID: 20076831
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question