0

I created a small app where the user can add number of instances and then add arbitrary numbers, making it all end up in a pandas df. However, attempting to challenge the user doing aforementioned process, the user ends up being able to complete the process of adding values. This should stop abruptly, if time deadline is not met.

Try run the code yourself:

import time
import pandas as pd


# define the countdown func.
def countdown(t):
    while t:
        mins, secs = divmod(t, 60)
        timer = '{:02d}:{:02d}'.format(mins, secs)
        print(timer, end="\r")


        a = int(input("Enter number of instances: "))
        test3 = []
        i = 0
        while i < a:
            pl = int(input("Enter arbitrary integer: "))
            test3.append(pl)
            i += 1
        print(list(test3))
        DF1 = pd.DataFrame(test3)
        return DF1


    time.sleep(1)
    t -= 1

    print('Game over')


# input time in seconds
t = input("Enter the time in seconds: ")

# function call
print(countdown(int(t)))

I'd suspect that I am missing an if-statement, but that is potentially what I need help doing or...?

Any help is appreciated...thx

EDIT:

import pandas as pd
from threading import Timer

timeout = 8

t = Timer(timeout, print, ['Game over'])
t.start()
prompt = "You have %d seconds to choose the correct answer...\n" % timeout
answer = input(prompt)

a=int(input("Enter number of instances: "))
test3=[]
i=0
while i < a:
    pl=int(input("Enter arbitrary integer "))
    test3.append(pl)
    i+=1

print(list(test3))
DF1 = pd.DataFrame(test3)
print(DF1)

t.cancel()
Newbie
  • 61
  • 1
  • 1
  • 6
  • When prompting for user input using `input()` , the program halts until the user responds, so I think you'd require multithreading to do what you want. That, or write a custom lower level version of the `input` function (this is what the package in [this github repo](https://github.com/johejo/inputimeout) does. This thread may be relevant as well: https://stackoverflow.com/questions/15528939/time-limited-input – jrbergen Nov 03 '21 at 16:20
  • Thx, looking into the potential answers to my problem I am a bit in doubt how to approach them, since I need to integrate my input function with the timer function, and this is basically where I am being challenged.... – Newbie Nov 03 '21 at 16:36
  • What specific approaches did you have in mind? If you specify your goal, and keep breaking these goals into smaller pieces, what problems do you encounter? – jrbergen Nov 03 '21 at 16:42
  • 1
    Leaving it as put in the original question, user is allowed to complete beyond timeout, and so if I try any of the solutions suggested, I do not succeed integrating my function with the solutions presented...my hope is that I work with a predefined time frame of, say 30 secs, then start the program with 'Enter number of instances' + 'Enter arbitrary integer'. If user does not keep the time, a text like 'Game over' should appear. So I want my function code to work directly with some sort of timeout function. Thx, for your advice...;o) – Newbie Nov 03 '21 at 16:51
  • If you try running the code presented in my question, you'll see what I mean. – Newbie Nov 03 '21 at 16:57
  • I ran the code before and I think I know what you mean, but I might have interpreted your first comment to mean that you were still stuck. – jrbergen Nov 03 '21 at 17:22
  • Actually, I did reach a solution like removing the def code and run it without, check edit code and run. Now I only need a way to end the program without an error. Run the code and reach limit viewing the 'Game over' and then press Enter. Then it fails. – Newbie Nov 03 '21 at 17:23

0 Answers0