How do I stop a separate function from running after my countdown timer function ends?

Viewed 30

I'm a complete beginner in programming and just wanted to code something I thought was interesting in python. I want to code the rest of it myself, but I got stumped on this part and cannot figure it out at all.

This is my code so far...

  • block, is the name I gave each session where users can input numbers
  • there are 3 blocks where users are given 5 seconds of time in each block to enter (as many sequences of "5" numbers) as they can.
  • each time the user enters a sequence of numbers, it should add those values to an index in the block_list. (doesn't matter if they enter 3 or 8 values)

Here's the problem: So after entering some numbers in, as soon as the timer runs out, I want the program to submit whatever the user is inputting to the list and skip to the next block iteration.

My code just doesn't do this and it also is stuck in a loop as well

Looking for help thanks!

Here is the output: OUTPUT

and here is the code:

from threading import Thread
import time
from random import *

block = 0
def main():
    global block
    while block < 3:
        Thread(target=userInputs).start()
        Thread(target=countTime(5)).start()
        block += 1

block_list = []
timeOut = True
def userInputs():
    while timeOut == True:
        num_inputs = int(input("Input 5 numbers and then press 'enter': "))
        block_list.append(num_inputs)
    print(block_list)


start_time = time.time()
num_list = [1,2,3,4]
block_list = []
def countTime(seconds):
    global timeOut
    global start_time
    while True:
        elapsed_time = time.time() - start_time
        if elapsed_time >= seconds:
            print()
            print("Time spent:")
            print(time.time()-start_time)
            timeOut = False
            break
    timeOut = True
    start_time = time.time()
    print(start_time)

main()
0 Answers
Related