需要获得定时输入以摆脱循环

时间:2016-08-05 22:38:15

标签: python loops timeout

我有一个用户输入的循环,当循环完成时我想要突破它:

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:
                break
            elif ord(chr) >= 32:
                input += chr
        if len(input) == 0 and (time.time() - start_time) > timeout:
            break

调用此循环的位置是:

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)
    raw_input()

    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)
            else:
                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)

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

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

如果按下所有正确的键并且运行状况为0或更低,我怎么能让这个循环爆发?

截至目前,它的确如此:

试图杀死山狮;出现时必须按正确的键  屏幕上。准备好后按Enter键

Enter: h
h: h
You slash at them with your knife doing 20
20
The mountain lion has 80 health remaining
Enter: a
a: a
You slash at them with your knife doing 24
24
The mountain lion has 56 health remaining
Enter: j
j: j
You slash at them with your knife doing 23
23
The mountain lion has 33 health remaining
Enter: p
p: p
You slash at them with your knife doing 10
10
The mountain lion has 23 health remaining
Enter: k
k: k
You slash at them with your knife doing 26
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
_input
    if msvcrt.kbhit():
KeyboardInterrupt

1 个答案:

答案 0 :(得分:0)

使用return转义battle - 函数。我的猜测就在这里

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

如果战斗成功,也许您想要返回当前的HP?通过简单地返回健康状况而不是什么,这应该很容易,即return healt而不仅仅是return

你可能也想在失去战斗后做同样的事情,但没有恢复任何健康状况,即return 0