3

i'm wondering how to accept input without the need to press enter. i searched online, and i get something regarding raw_input, but i think that became obsolete after the arrival of python 3.0. sometimes, i run a while loop on a whole program since i want to ask the user: continue? (y/n):

for instance consider the code:

import random

d = input('Toss coin? (y/n): ')

while d != 'n' and d!= 'N':
    c = random.randint(1,2)
    if c == 1:
        print('HEADS!')
    else:
        print('TAILS!')

    d = input('Toss coin? (y/n): ')

but i just want to add more flare to my program by just not having the user press enter everytime. just press y or n and the program loops or breaks accordingly.

ok so this is the new code:

import random
import msvcrt

d = input('Toss coin? (y/n): ')

while d != 'n' and d!= 'N':
    c = random.randint(1,2)
    if c == 1:
        print('HEADS!')
    else:
        print('TAILS!')

    print('Toss coin? (y/n): ')
    d = msvcrt.getwch()

but msvcrt still doesn't work

amin
  • 429
  • 4
  • 8
  • 15

1 Answers1

6

If you are using windows, msvcrt is the answer:

import msvcrt

print ("Please enter a value.")
char = msvcrt.getch()
print char

If you are not using windows, take a look at the following snippet at this source:

getch = _Getch()
print ("Please enter something: ")
x = getch()
print x
LPH
  • 1,275
  • 9
  • 16
  • do i have to import something for your code to work? – amin Dec 30 '13 at 00:40
  • it's not working... says invalid syntax – amin Dec 30 '13 at 00:40
  • ok i understand that perhaps _getch() only works on unix based systems – amin Dec 30 '13 at 00:42
  • however, why do i have to use microsoft's library to do something in python? isn't there any way using only python? I remember being able to do somehting like this in java – amin Dec 30 '13 at 00:43
  • 2
    nope, it works on both Windows and Unix!- more information [here](http://code.activestate.com/recipes/134892/) – LPH Dec 30 '13 at 00:43
  • yea, i know, in java i could offer you more solutions- but in python my knowledge is limited... – LPH Dec 30 '13 at 00:44
  • thanks buddy, works perfectly. i'll mark your answer right in 5 min – amin Dec 30 '13 at 00:45
  • 4
    Just note that this obviously won't work in IDLE or any other environment that uses pythonw.exe or embeds python in a process that lacks a console. – Eryk Sun Dec 30 '13 at 00:46
  • ok thanks. i just spotted a problem also... when i use msvcrt in my code, the loop doesn't run but just prints what i entered as input – amin Dec 30 '13 at 00:48
  • @amin: `getch` returns `bytes`. Use `getwch`. – Eryk Sun Dec 30 '13 at 00:54
  • hmm same thing happens – amin Dec 30 '13 at 00:56
  • i posted new code under the old one. help please! – amin Dec 30 '13 at 00:57
  • how does the loop not run? is there an error message? – LPH Dec 30 '13 at 00:57
  • suppose i enter 'y', the program terminates displaying the characater y. – amin Dec 30 '13 at 00:58
  • and you are using the follwing code? : d = msvcrt.getch() – LPH Dec 30 '13 at 01:00
  • tried with both msvcrt.getch() and msvcrt.getwch() as recommended by @eryksun – amin Dec 30 '13 at 01:01
  • try to print the value you have captured in d in the line after the msvcrt.getch(); what does it say? – LPH Dec 30 '13 at 01:02
  • program still terminates and nothing prints. i am using eclipse by the way – amin Dec 30 '13 at 01:04
  • hm...now you've got me clearly stumped... this could eventually solve your problem: [Using msvcrt.getch() in Eclipse](http://stackoverflow.com/questions/16076853/using-msvcrt-getch-in-eclipse-pydev) – LPH Dec 30 '13 at 01:08
  • 1
    seems to be a known issue: [Bug 375172 - msvcrt.getch() isn't parsed from Eclipse console](https://bugs.eclipse.org/bugs/show_bug.cgi?id=375172) – LPH Dec 30 '13 at 01:10
  • ah ok. so what i'm doing is correct, but however it does not work on eclipse right? thanks! – amin Dec 30 '13 at 01:13
  • 2
    @amin, as I said it needs a console window. You could use ctypes to call `kernel32.AllocConsole`, and then open the special file `"CONOUT$"` to print to it, but you'd have to type into the console window for `getwch` to get the keyboard input messages. – Eryk Sun Dec 30 '13 at 01:17
  • hmm this seems quite complicated.. i'm just new to python.. i'll read up some more on what you mentioned. thanks – amin Dec 30 '13 at 01:20