Python Forum

Full Version: Problems with Equality of Numbers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have made code to help study multiplication facts but I am having some problems I need fixed. First off and the easiest is that I enter the same number loaded into the variable but it says that they are not the same. If that was hard to understand just take a look. 2. I need a way to only give someone 3 seconds to answer instead of all the time they need.
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]
variable = 0
print('You have 3 seconds to answer each question')
time.sleep(3)               
for x in range(0,10):
    print('Ready...')
    time.sleep(1)
    print('Set...')
    time.sleep(1)
    print('Go!!!')
    time.sleep(1)
    Number_0 = random.choice(Number_0_list)
    Number_1 = random.choice(Number_1_list)
    print(Number_0, '*', Number_1)
    Answer = Number_0 * Number_1
    variable_record = variable
    InputAnswer = input("Answer Here: ")
    if InputAnswer == Answer:
        print('Nice Job!')
        time.sleep(1)
    else:
        print('OOF! Better luck next time')
        time.sleep(1)
        print('The real answer is %s' % Answer)
Output:
You have 3 seconds to answer each question Ready... Set... Go!!! 1 * 5 Answer Here: 5 OOF! Better luck next time The real answer is 5 Ready... Set... Go!!! 6 * 11 Answer Here: 66 OOF! Better luck next time The real answer is 66 Ready... Set... Go!!! 0 * 9 Answer Here:
Try this. A string of letters can never equal a number.

    InputAnswer = input("Answer Here: ")
    if InputAnswer == Answer:
        print('Nice Job!')
        time.sleep(1)
    else:
        print('OOF! Better luck next time')
        print(type(InputAnswer), type(Answer))
        time.sleep(1)
        print('The real answer is %s' % Answer)  
For the time limit, you'll need multithreading and set up a race condition. Essentially, you need two threads - one for user input and one for the timer. The timer thread would send a null answer after three seconds once activated. So, the user will have to provide an answer before the timer does.
I posted code here to take user input with a timeout. You could use it here.