c = raw_input("Press s or n to continue:")if c.upper() == "S": print "YES"It functions as intfinished, yet the user has to press enter in the shell after pushing "s". Is there a means to achieve what I need from an user input without needing to push enter in the shell? I"m utilizing *nixes machines.

You watching: Python input without enter


Under Windows, you require the msvcrt module, especially, it seems from the way you explain your problem, the feature msvcrt.getch:

Read a keypress and also rerevolve the resulting character. Nopoint is echoed to the console. This contact will block if a keypress is not currently available, yet will not wait for Enter to be pressed.

(etc -- watch the docs I simply pointed to). For Unix, watch e.g. this recipe for a straightforward means to construct a similar getch function (watch likewise a number of alternatives &c in the comment thcheck out of that recipe).

See more: Review: Game Changer Book Review, Game Changer: 9780061998676: Shusterman


Python does not carry out a multiplatform solution out of package.If you are on Windows you could attempt msvcrt with:

import msvcrtprint "Press s or n to continue: "input_char = msvcrt.getch()if input_char.upper() == "S": print "YES"


Actually in the meantime (almost 10 years from the start of this thread) a cross-platcreate module named pynput appeared.Below a first cut - i.e. that works with lowercase "s" just.I have actually tested it on Windows but I am almost 100% positive that it need to job-related on Linux.

from pynput import keyboardprint("Press s or n to continue:")via key-board.Events() as events: # Block for as much as feasible event = events.get(1e6) if occasion.key == key-board.KeyCode.from_char("s"): print("YES")


curses deserve to carry out that as well :

import curses, timedef input_char(message): try: win = curses.initscr() win.addstr(0, 0, message) while True: ch = win.getch() if ch in range(32, 127): break time.sleep(0.05) finally: curses.endwin() rerotate chr(ch)c = input_char("Do you desire to continue? y/")if c.lower() in <"y", "yes">: print("yes")else: print("no (gained )".format(c))


Instead of the msvcrt module you can likewise usage WConio:

On a side note, msvcrt.kbhit() returns a boolean worth determining if any vital on the keyboard is currently being pressed.

So if you"re making a game or somepoint and desire keypresses to do points yet not halt the game entirely, you have the right to use kbhit() inside an if statement to make sure that the crucial is just retrieved if the user actually wants to execute somepoint.

An example in Python 3:

# this would certainly be in some sort of check_input functionif msvcrt.kbhit(): key = msvcrt.getch().decode("utf-8").lower() # getch() returns bytes data that we must decode in order to review properly. i additionally compelled lowerinstance which is optional but recommfinished if crucial == "w": # right here "w" is offered as an instance # execute stuff elif key == "a": # execute other stuff elif crucial == "j": # you gain the allude
I know this is old, however the solution wasn"t good sufficient for me.I need the solution to assistance cross-platform and also without installing any kind of outside Python packages.

My solution for this, in case anyone else comes across this post


from tkinter import Tk, Framedef __set_key(e, root): """ e - event through attribute "char", the released key """ worldwide key_pressed if e.char: key_pressed = e.char root.destroy()def get_key(msg="Press any kind of vital ...", time_to_sleep=3): """ msg - set to empty string if you don"t desire to print anypoint time_to_sleep - default 3 seconds """ worldwide key_pressed if msg: print(msg) key_pressed = None root = Tk() root.overrideredirect(True) framework = Frame(root, width=0, height=0) structure.bind("", lambda f: __set_key(f, root)) frame.pack() root.focus_set() frame.focus_set() structure.focus_force() # doesn"t occupational in a while loop without it root.after(time_to_sleep * 1000, func=root.destroy) root.mainloop() root = None # just in instance rerotate key_presseddef __main(): c = None while not c: c = get_key("Choose your weapon ... ", 2) print(c)if __name__ == "__main__": __main()