7

If you wait out the 4 seconds it says "You ran out of time" which is good. But then, to keep the loop going, you will have to press the enter key to continue.

I want so that when it prints "You ran out of time" underneath instead of just typing, that it displays an input statement like "Type 'attack' to keep going" and the loop would continue from where it was.

from threading import Timer
import time

monsterhp = int(800)
y = 150
while monsterhp > 0:
    timeout = 4
    t = Timer(timeout, print, ['You ran out of time.'])
    t.start()
    print(" ")
    prompt = "You have %d seconds Type 'attack' to hit the monster\nType here: " % timeout
    answer = input(prompt)
    t.cancel()

    if answer == "attack":
        print("You strike the monster")
        time.sleep(1)
        monsterhp = monsterhp - y
        print("War Lord Health:", monsterhp)
Taku
  • 31,927
  • 11
  • 74
  • 85
mykill456
  • 91
  • 1
  • 7

3 Answers3

3

Doing the task you proposed isn't as easy as you might've guessed. It is easier to use the signal module to do this: (I have incorporated your code with a modified version of the answer I linked)

import signal, time

def TimedInput(prompt='', timeout=20, timeoutmsg = None):
    def timeout_error(*_):
        raise TimeoutError
    signal.signal(signal.SIGALRM, timeout_error)
    signal.alarm(timeout)
    try:
        answer = input(prompt)
        signal.alarm(0)
        return answer
    except TimeoutError:   
        if timeoutmsg:
            print(timeoutmsg)
        signal.signal(signal.SIGALRM, signal.SIG_IGN)
        return None

monsterhp = int(800)
y = 150
while monsterhp > 0:
    timeout = 4
    timeoutmsg = 'You ran out of time.'
    print(" ")
    prompt = "You have %d seconds Type 'attack' to hit the monster\nType here: " % timeout
    answer = TimedInput(prompt, timeout, timeoutmsg)

    if answer == "attack":
        print("You strike the monster")
        time.sleep(1)
        monsterhp = monsterhp - y
        print("War Lord Health:", monsterhp)

Note: this will only work on all unix/mac system

You can change your while loop to this, for a improved version of your code:)

while monsterhp > 0:
        timeout = 4
        timeoutmsg = 'You ran out of time.'
        print(" ")
        prompt = "You have %d seconds Type 'attack' to hit the monster\nType here: " % timeout
        answer = TimedInput(prompt, timeout, timeoutmsg)

        if answer == "attack":
            print("You strike the monster")
            time.sleep(1)
            monsterhp = monsterhp - y
            print("War Lord Health:", monsterhp)
        elif answer == None:
            print("The War Lord has killed you, you're now dead")
            print("Thanks for playing, \nGAME OVER")
            break
Community
  • 1
  • 1
Taku
  • 31,927
  • 11
  • 74
  • 85
  • wow I wasn't expecting that at all. By the way I simplified that version of my code so it would be more understandable. https://pastebin.com/mbYgyucJ . Which is close to your second answer aha. But I will have to test the TimedInput in another hour just busy atm. thanks for reply anyway. – mykill456 May 18 '17 at 04:48
  • Wow thanks it worked I cant believe there is so much code. Once again thanks – mykill456 May 18 '17 at 05:15
2

There is a new library inputimeout for standard input with timeout

$ pip install inputimeout

usage

from inputimeout import inputimeout, TimeoutOccurred
try:
    string = inputimeout(prompt='>>', timeout=5)
except TimeoutOccurred:
    string = 'time is over'
print(string)
sound wave
  • 3,191
  • 3
  • 11
  • 29
  • 1
    This doesn't work. This always times out and doesn't check for the value entered BEFORE the timeout. – Abhishek Rai Feb 18 '21 at 14:00
  • @AbhishekRai you are doing something wrong, it works perfectly, try setting a longer timeout and after writing the input remember to press enter. Share your code – sound wave Feb 18 '21 at 22:55
  • 1
    Let me try that again. Nopes..Fails. This is what works..on windows. https://stackoverflow.com/questions/66262161/set-value-of-variable-if-nothing-is-entered-python-windowstimed-input/66265390#comment117154381_66265390 – Abhishek Rai Feb 19 '21 at 07:46
  • @AbhishekRai i'm on windows 10 and `inputimeout` works like a charm, see picture https://i.imgur.com/6iEH9T6.png – sound wave Feb 19 '21 at 11:52
-1
import datetime

def custom_time_input(msg, seconds):
    try:
        print(msg)
        # current time in seconds
        current_time = datetime.datetime.now()
        time_after = current_time + datetime.timedelta(seconds=seconds)
        while datetime.datetime.now() < time_after:
            print("Time left: ", end="")
            print(time_after - datetime.datetime.now(), end="\r")
            time.sleep(1)
        print("\n")
        return True
    except KeyboardInterrupt:
        return False

res = custom_time_input("If you want to create a new config file PRESS CTRL+C within 20 seconds!", 20)
if res:
    pass # nothing changed
else:
    pass # do something because user pressed ctrl+c

> Blockquote
Dipesh Paul
  • 439
  • 4
  • 9