Mr_links
Mr_links

Reputation: 35

How to exit out of a raw input prompt

I'm trying to this block of code to work but when I try to use quit() the prompt for the raw_input is still there is it possible to end it?

import threading
import sys

def monsterHit():
    print "you died"
    sys.exit(0)

t = threading.Timer(1.25, monsterHit)
t.start() 

print "Type \"jump\" before the monster charges you! "
jumpCheck = raw_input("> ")
if jumpCheck == "jump":
    t.cancel()
    print"you dodged,good job!"
else:
     monsterHit()

Upvotes: 1

Views: 495

Answers (1)

Jonas Wolff
Jonas Wolff

Reputation: 2244

I did som digging and got to the solution of just recording the key strokes i wouldn't call this optimal but i think it some what solves your problem :/

import sys
import time
import msvcrt

def monsterHit():
    print("you died")
    sys.exit(0)

print("Type \"jump\" before the monster charges you! ")

jumpCheck = "> "
timer = time.time()
while 1.25 >= time.time()-timer:
    if msvcrt.kbhit():
        key = msvcrt.getch()
        if key == b'\r':
            break
        jumpCheck += key.decode("utf-8")
        sys.stdout.flush()
        sys.stdout.write("\r"+jumpCheck)

print("")
if jumpCheck == "> jump":
    print("you dodged,good job!")
else:
     monsterHit()

oh and as a side note, i really think 1.25 seconds is way to fast, i could only pass it knowing what to type xD

Upvotes: 1

Related Questions