Python Forum
Help with Stopping a function after a timer
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Stopping a function after a timer
#1
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using threading.Timer for function korenron 1 1,154 Dec-20-2022, 01:09 PM
Last Post: ndc85430
Question Stopping a parent function from a nested function? wallgraffiti 1 3,617 May-02-2021, 12:21 PM
Last Post: Gribouillis
  email timer/rss feed timer ndiniz 1 2,040 Feb-02-2021, 07:18 PM
Last Post: nilamo
  stopping number conversion before end Skaperen 6 2,941 Jul-12-2020, 09:22 AM
Last Post: DeaD_EyE
  Stopping a loop in another file catosp 4 2,597 Jun-15-2020, 02:45 PM
Last Post: catosp
  Run a timer when a functions starts to see how long the function takes to complete Pedroski55 2 1,948 Apr-19-2020, 06:28 AM
Last Post: Pedroski55
  Cannot use function with timer/clock theangryprogrammer 1 3,288 Jan-22-2019, 04:22 PM
Last Post: Larz60+
  reading raw data until condition is met, then stopping unknowntothem 7 4,070 Sep-27-2018, 06:10 AM
Last Post: unknowntothem
  Is there a max value for threading.Timer function? whookie 1 3,919 Feb-03-2018, 09:51 PM
Last Post: ka06059
  Stopping scheduler with condition j.crater 17 15,734 Jan-10-2017, 11:06 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020