num
is not actually used in multiply()
so there is no reason to pass it in. Instead, declare multiply()
without the argument:
def multiply():
'''(num1 * num2)'''
.
.
And call it from __main__
like this:
if maths == "Multiplication" or "m" or "x":
multiply()
There seems no point in checking within multiply()
whether it is supposed to perform multiplication as does this line:
if maths == "Multiplication" or "m" or "x":
And you try to call multiply()
recursively, which will fail:
if ans == multiply(num1, num2):
… just use the *
operator.
Finally, why return the result of the multiplication? What use is the product if the multiplicands are not known outside of function multiply()
? It might be better to return whether the user got the correct answer.
Putting all of the above together, you get this:
import random
def multiply():
'''(num1 * num2)'''
num1 = random.randint(1,12)
num2 = random.randint(1,12)
ans = int(input("What is the answer to " + str(num1) + " x " + str(num2) + " ? "))
correct = (ans == num1 * num2)
if correct:
print("You are correct! ")
else:
print("Wrong, please try again. ")
return correct
if __name__ == '__main__':
name = input("What is your name? ")
maths = input("What mathematics would you like to learn, " + name + "? ")
if maths == "Multiplication" or "m" or "x":
correct = multiply()
solved NameError: name ‘num’ is not defined