YoYoYo I'm Awesome YoYoYo I'm Awesome - 2 months ago 7
Python Question

Need to get timed input to break out of loop

I have a loop that times user input, when the loop is done I want to break out of it:

import sys
import time
import msvcrt
from random import randint
import random

def time_input(caption, timeout=3):
start_time = time.time()
sys.stdout.write('%s: ' % (caption))
input = ''
while True:
if msvcrt.kbhit():
chr = msvcrt.getche()
if ord(chr) == 13:
elif ord(chr) >= 32:
input += chr
if len(input) == 0 and (time.time() - start_time) > timeout:

Where this loop is called, is here:

def battle(animal, weapon, health):
print "To try to kill the {};" \
" you must press the correct key" \
" when it appears on the screen." \
" Press enter when ready".format(animal)

keys = 'abcdefghijklmnopqrstuvwxyz'
animal_health = 100

while health > 0:
while True:

if animal == 'mountain lion':
animal_damage = randint(27, 97)
animal_output = "The mountain lion slashes at you doing {} damage!".format(animal_damage)
animal_damage = randint(10, 25)
animal_output = "The {} slashes at you doing {} damage!".format(animal, animal_damage)

correct_key = random.choice(keys)
print "Enter: {}".format(correct_key)
to_eval = time_input('> ')

if to_eval == correct_key:
damage = weapon_info(weapon, animal_health)
print damage
animal_health -= damage
print "The {} has {} health remaining".format(animal, animal_health)
if animal_health <= 0:
win_battle(animal, weapon)

print animal_output
health -= animal_damage
print "You have {} health remaining".format(health)
if health <= 0:

battle('mountain lion', 'knife', 100)

How can I get this loop to break out when all the correct keys have been pressed and the health is 0 or less?

As of now it does this:

To try to kill the mountain lion; you must press the correct key when it appears
on the screen. Press enter when ready

Enter: h
h: h
You slash at them with your knife doing 20
The mountain lion has 80 health remaining
Enter: a
a: a
You slash at them with your knife doing 24
The mountain lion has 56 health remaining
Enter: j
j: j
You slash at them with your knife doing 23
The mountain lion has 33 health remaining
Enter: p
p: p
You slash at them with your knife doing 10
The mountain lion has 23 health remaining
Enter: k
k: k
You slash at them with your knife doing 26
The mountain lion has -3 health remaining
You pick up the dead mountain lion and start walking back to camp.
You found extra meat!
Enter: h # Just keeps going
h: Traceback (most recent call last):
File "battle.py", line 97, in <module>
battle('mountain lion', 'knife', 100)
File "battle.py", line 31, in battle
to_eval = time_input(correct_key)
File "C:\Users\thomas_j_perkins\bin\python\game\settings.py", line 36, in time
if msvcrt.kbhit():


Use return to escape the battle-function. My guess is right here

if animal_health <= 0:
    win_battle(animal, weapon)
    return     <------------------------------

Maybe you want to return your current hp if the battle was successful? This should be easy by simply returning health instead of nothing, i.e. return healt instead of just return

You probably want to do the same thing after loosing a battle as well, but without returning any health, i.e. return 0?