Python Forum
Problems with Equality of Numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with Equality of Numbers
#1
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:
Reply
#2
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)  
Reply
#3
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.
Reply
#4
I posted code here to take user input with a timeout. You could use it here.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Checking for equality PythonGainz 8 3,149 Apr-10-2020, 03:00 PM
Last Post: PythonGainz
  Equality in Python el_bueno 8 3,354 Feb-08-2020, 03:33 PM
Last Post: el_bueno
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,662 May-09-2019, 12:19 PM
Last Post: Pleiades
  Dictionnaries - Equality SupaFlamme 3 2,691 Jan-13-2019, 04:50 PM
Last Post: perfringo
  Shared reference and equality zyo 3 3,096 Jun-30-2018, 07:10 PM
Last Post: ljmetzger

Forum Jump:

User Panel Messages

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