Bottom Page

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()
Age Doesn't Matter, Your Passion Does
- Shreya Jain
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  AssertionError: View function mapping is overwriting an existing endpoint function Zhavi221 7 1,233 Apr-17-2019, 01:07 PM
Last Post: Zhavi221
  Timer class not working as expected. MuntyScruntfundle 4 346 Feb-02-2019, 09:47 AM
Last Post: MuntyScruntfundle
  code keeps running if i use from threading import timer? birddseedd 3 282 Jan-25-2019, 05:00 AM
Last Post: stullis
  Cannot use function with timer/clock theangryprogrammer 1 351 Jan-22-2019, 04:22 PM
Last Post: Larz60+
  reading raw data until condition is met, then stopping unknowntothem 7 539 Sep-27-2018, 06:10 AM
Last Post: unknowntothem
  Run function in parallel but inherite dynamic data from the previous function atizva 4 762 Jul-11-2018, 06:39 AM
Last Post: volcano63
  How do I create a timer that counts down? LavaCreeperKing 4 602 Jun-20-2018, 05:23 PM
Last Post: LavaCreeperKing
  code for a timer SheeppOSU 2 687 Feb-20-2018, 11:01 AM
Last Post: buran
  Is there a max value for threading.Timer function? whookie 1 1,258 Feb-03-2018, 09:51 PM
Last Post: ka06059
  View function mapping is overwriting an existing endpoint function: index rarevesselt 12 11,435 Oct-22-2017, 12:34 AM
Last Post: rarevesselt

Forum Jump:


Users browsing this thread: 1 Guest(s)