0

I would like to be able to receive command line input from user in a python script, and at the same time display to the user some dynamic information. The user should be able to enter text, but this should not block the displaying of information.

My goal is to create a game, where I show users a countdown while they still have time to enter an answer.

Is this achievable?

Ray P.
  • 875
  • 1
  • 9
  • 24

2 Answers2

0

Yeah. To create a countdown in the console, you could do something like this:

from time import sleep
for num in reversed(range(0,11)):
    print(num)
    sleep(1.0)

or

from time import sleep
time = 10
while time != 0:
    print(time)
    time = time - 1
    sleep(1.0)

Either will countdown from 10 to 0 with a second in between each number. Since you might want the user to be able to enter answers as quickly or slowly as like before reaching 0... you might want to look into running two loops concurrently. this thread might be helpful for that. You'll want to figure out how to break out of both loops if the user gets the right answer (and have something come up that says they got the right answer) or if the user runs out of time.

Dave Rosenman
  • 1,252
  • 9
  • 13
  • 1
    But it would block command line input. And I want the displayed countdown be updated in place, not like: 10 9 8 7 ... – Ray P. Dec 11 '17 at 19:40
  • Oh. I think you might want to check out "pygame". [pygame.org](http://www.pygame.org) – Dave Rosenman Dec 11 '17 at 20:34
  • Thank you. Does pygame apply to command line interfaces? I thought it's more for GUI and OpenGL stuff. I'll research it though. – Ray P. Dec 12 '17 at 20:04
0

Well sounded like an interesting thing to look into so I did, ran into a few problems pretty soon.

First, I was able to make a running counter but the problem is, since it is a loop, the next layer the counter loop will reset everything you've answered on the previous layer unless you've pressed enter to input answer(answer + enter , during that 1 second period).

if you are making reflex based thing that you only need to press single keys you might be able to succeed with my code by using module getch.

There were few modules that I could use for making the timer and program run at the same time, threading and multiprocessing(better results with threading).

It's basically a working thing if you just remove the counter function which adds the loop, but you won't see the timer.

Pygame might be a good thing to do this with, haven't used it myself but I'd presume so.

here is my code

import time
import threading
import os


timel = 5

def question():
    q = input("",)
    print(q)
    if q == "2":
        print("\nCorrect")
    else:
        exit("\nFalse, You lose!!")

def counter():
    timel = 5
    for i in range(0, timel):
        print("How much is 1+1?", timel)
        timel -= 1
        time.sleep(1)
        os.system("cls")


def timer(seconds):
    time.sleep(seconds)
    exit("\nTIMES UP, You lose!!")

def exit(msg):
    print(msg)
    os._exit(1)

def main():
    thread = threading.Thread(target=timer, args=(int("{}".format(timel)),))
    thread2 = threading.Thread(target=counter)
    thread.start()
    thread2.start()
    question()

if __name__ == "__main__":
    main()
  • 1
    Thanks. That's not exactly it though. I too have my working implementation that even allows me to stop asking user for input when the time is gone, but the part with displaying a countdown in-place is still unresolved. There are many of apps that run in console (not necessarily written with python) where things are updated dynamically (htop is a good example of what I mean), and I just thought that there must be a way to create such interfaces with Python. – Ray P. Dec 12 '17 at 20:09