Python Forum
Help with Stopping a function after a timer - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help with Stopping a function after a timer (/thread-15719.html)



Help with Stopping a function after a timer - SheeppOSU - Jan-28-2019

So I setup this multiplication expression thing. I setup a function to run the expression and a timer for the expression. I'm trying to only allow the person 5 seconds to answer and this is what I've got to, except it won't end the function after 5 seconds. Either it's not running both functions simultaneously, or the end module isn't working because it typed it incorrectly.
import multiprocessing as mp
import random
import time

Number_0_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Number_1_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Number_0 = random.choice(Number_0_list)
Number_1 = random.choice(Number_1_list)


print('You have 3 seconds to answer each question, type "startgame()" when your ready')
time.sleep(3)               

def startprogram(times):
    check = 0
    for x in range(0, times):
        Answered = 0
        check = check + 1
        print('Ready...')
        time.sleep(1)
        print('Set...')
        time.sleep(1)
        print('Go!!!')
        time.sleep(1)

        print(Number_0, '*', Number_1)

        timerstart = 1
        InputAnswer = input("Answer Here: ")
        timerstart = 0

        Answer = Number_0 * Number_1
        IntAnswer = (int(float(Answer)))
        IntInputAnswer = (int(float(InputAnswer)))

        if IntInputAnswer == IntAnswer:
            Answered = 1
            print('Nice Job!')
            time.sleep(1)
        else:
            print('OOF! Better luck next time')
            print(type(IntInputAnswer), type(IntAnswer))
            time.sleep(1)
            print('The real answer is %s' % IntAnswer)

def timer():
    while True:
        if timerstart == 1:
            time.sleep(5)
            if Answered == 0:
                startprogram.end()
                startprogram(check).start()
def startgame():
    if __name__=='__main__':
        p1 = mp.Process(target = startprogram(10))
        p1.start()
        p2 = mp.Process(target = timer())
        p2.start()
        p1.join()
        p2.join()