I've been making a program to practice some multiplication because I've never been taught that and want to improve it for faster math and school, but for some reason, there are random chances it either is random, or it just gets an error after twelve cycles. Here's the code:
import random
import time
#twelves
def qone():
one = input("twelve times one ")
if one == "12":
print("correct")
def qtwo():
two = input("twelve times two ")
if two == "24":
print("correct")
def qthree():
three = input("twelve times three ")
if three == "36":
print("correct")
def qfour():
four = input("twelve times four ")
if four == "48":
print("correct")
def qfive():
five = input("twelve times five ")
if five == "60":
print("correct")
def qsix():
six = input("twelve times six ")
if six == "72":
print("correct")
def qseven():
seven = input("twelve times seven ")
if seven == "84":
print("correct")
def qeight():
eight = input("twelve times eight ")
if eight == "96":
print("correct")
def qnine():
nine = input("twelve times nine ")
if nine == "108":
print("correct")
def qten():
ten = input("twelve times ten ")
if ten == "120":
print("correct")
def qeleven():
eleven = input("twelve times eleven ")
if eleven == "132":
print("correct")
def qtwelve():
twelve = input("twelve times twelve ")
if twelve == "144":
print("correct")
#loop random questions
while True:
random.random(qone(), qtwo(), qthree(), qfour(), qfive(), qsix(), qseven(), qeight(), qnine(), qten(), qeleven(), qtwelve())
I've seen a lot about tables too, but I'm a beginner so I don't know how they work and I'm a beginner.
CodePudding user response:
This will run a random function from the list forever. Replace your while loop with this code.
functions = [qone, qtwo, qthree, qfour, qfive, qsix, qseven, qeight, qnine, qten, qeleven, qtwelve]
#loop random questions
while True:
random.choice(functions)()
CodePudding user response:
The existing answers to this question cover better ways to write this code, so I'll just focus on explaining why you're seeing the behavior you're seeing.
This line doesn't do anything random at all -- it in fact does the exact same thing each time your program runs:
random.random(qone(), qtwo(), qthree(), qfour(), qfive(), qsix(), qseven(), qeight(), qnine(), qten(), qeleven(), qtwelve())
Whenever you call a function, its parameters are evaluated before the function is called. So here's the sequence of what happens when this line is executed:
qone() # returns None
qtwo() # returns None
... # (nine more function calls, all returning None)
qtwelve() # returns None
random.random(None, None, ..., None)
What you're seeing when you run your program is that each question is asked, in order (because the parameters are evaluated in order), and then the program halts with an error because you're calling random.random()
with unexpected parameters. All of those things happen on that same line of code, in the very first iteration of your while
loop.
CodePudding user response:
The repetitive functions with hardcoded answers will not extend to multiplications by other numbers. Luckily the repetition is fairly easy to condense to one function, if you can accept not using English words for the numbers (there's code for turning integers into English words online, but I don't add that here because it would distract from the problem).
import random
def q(a: int, b: int):
if str(a*b) == input(str(a) " times " str(b) "?: "):
print("correct!")
else:
print("incorrect...")
def loopq(a: int, b_low: int, b_high: int):
while True:
b = random.randint(b_low, b_high)
q(a, b)
loopq(12, 1, 12) # equivalent to your code