Maths quiz question

i really like to code but i have been given a assignment where i must generae a random number with a random operation (+,-,* ect) but when i run it it says that what the user answered is a wrong answer no matter what if entered, i really am at the end of my teather and am close to giving up, any help is greatly appreceated, thanks

import random
num = random.randint(1,10)
num2 = random.randint(1,10)
def Addition():
num = random.randint(1,10)
num2 = random.randint(1,10)
answer = num + num2
Question = input ("What is %s + %s? " % (num,num2))
if Question == answer:
print(“Well done!”)
elif Question != answer:
print(“Sorry, that is incorrect!”)
def Subtraction():
num = random.randint(1,10)
num2 = random.randint(1,10)
answer = num - num2
Question = input ("What is %s - %s? " % (num,num2))
if Question == answer:
print(“Well done!”)
elif Question != answer:
print(“Sorry, that is incorrect!”)
def Multiplication():
num = random.randint(1,10)
num2 = random.randint(1,10)
answer = num * num2
Question = input ("What is %s * %s? " % (num,num2))
if Question == answer:
print(“Well done!”)
elif Question != answer:
print(“Sorry, that is incorrect!”)
Operators = random.randint(1,3)
if Operators == 1:
Addition()
if Operators == 2:
Subtraction()
if Operators == 3:
Multiplication()

@cloudjumper36761 you had some syntax issues. But I think the main problem was that python was understanding question as being a string, and when it was comparing question and answer in the if statement, it was giving a false, since one was a string and the other integer.

I rewrote your code a bit and added an int() for you input for the question. And this worked for me.

Also you shouldn’t declare num and num2 outside of the functions and in each function as well. Just have either global variables outside of the functions(like in my solution) or local ones in each function.

import random

def addition():
    answer = num + num2
    question = int(input ("What is %s + %s? " % (num,num2)))
    
    if answer == question:
        print("Well done!")
    else:
        print("Sorry, that is incorrect!")
        
def subtraction():
    answer = num - num2
    question = int(input ("What is %s - %s? " % (num,num2)))
    
    if answer == question:
        print("Well done!")
    else:
        print("Sorry, that is incorrect!")
        
def multiplication():
    answer = num * num2
    question = int(input ("What is %s * %s? " % (num,num2)))
   
    if answer == question:
        print("Well done!")
    else:
        print("Sorry, that is incorrect!")
        
num = random.randint(1,10)
num2 = random.randint(1,10)
operators = random.randint(1,3)

if operators == 1:
    addition()
elif operators == 2:
    subtraction()
else:
    multiplication()

Hope this helps. Feel free to reply in case you have questions.

P.S. for the future it would be really nice to have the codes in your posts with indentations. Here is a link to guide you.

Look for this section.

I got carried away a bit, and made you code shorter. You can check this as well :wink:

import random

def myFunc():
    if operators == 1:
        answer = num + num2
        question = int(input ("What is %s + %s? " % (num,num2)))
    elif operators == 2:
        answer = num - num2
        question = int(input ("What is %s - %s? " % (num,num2)))
    else:
        answer = num * num2
        question = int(input ("What is %s * %s? " % (num,num2)))
        
    if answer == question:
        print("Well done!")
    else:
        print("Sorry, that is incorrect!")


num = random.randint(1,10)
num2 = random.randint(1,10)
operators = random.randint(1,3)

myFunc()

actually as i dont think asking this question is worth adding another post do you know how to create a while loop within this that would stop once it’s asked 10 questions, I have tried doing
for i in range(10):
myFunc()
yet this prints the same question 10 times, any help?

import random

def myFunc():
    if operators == 1:
        answer = num + num2
        question = int(input ("What is %s + %s? " % (num,num2)))
    elif operators == 2:
        answer = num - num2
        question = int(input ("What is %s - %s? " % (num,num2)))
    else:
        answer = num * num2
        question = int(input ("What is %s * %s? " % (num,num2)))
        
    if answer == question:
        print("Well done!")
    else:
        print("Sorry, that is incorrect!")


i = 0
while i < 10:
     
    num = random.randint(1,10)
    num2 = random.randint(1,10)
    operators = random.randint(1,3)

    myFunc()
    i +=1

Hey, here is a solution to your while loop question.)))

sorry dude for asking so many questions but i am now trying to create a score to tell the user what they got at the end of the test, i have got to the point that it displays 0/10 so far but it is not working

also (just a general question) how to you make somthing inside the myFunc() thing affect a variable that is outside of it?
(sorry if you dont understand because of my wording) and thanks for your continuous help :smile:

Hey, it’s not a problem )) Here is a solution to count the score.

import random

def myFunc():
    
    if operators == 1:
        answer = num + num2
        question = int(input ("What is %s + %s? " % (num,num2)))
    elif operators == 2:
        answer = num - num2
        question = int(input ("What is %s - %s? " % (num,num2)))
    else:
        answer = num * num2
        question = int(input ("What is %s * %s? " % (num,num2)))
        
    if answer == question:
        print("Well done!")
        return True
    else:
        print("Sorry, that is incorrect!")
    


testScore = 0
i = 0
while i < 10:
     
    num = random.randint(1,10)
    num2 = random.randint(1,10)
    operators = random.randint(1,3)

    correct = myFunc()
    if correct == True:
        testScore +=1
    
    i +=1
    
print("You final score is %s/10" % testScore)

And about the variables question, I believe that something declared outside a function is a global variable, hence can be modified inside the function as well. But the variables declared inside the function are local, and you cannot use them outside the function.
I am not that knowledgable. However, you can check it out for more details here:

1 Like

that was useful thanks dude, ill just say what i need for the final thing as i feel that i’m wasting your time :confused:
the program is working so far but after this i need to assign the users name, their score and their class into one variable or 1 way which i then need to display into a exel file with the class, name, and score coloms, and at the end it should give them an option to repeat it from when the 10 questions start and then i need it to create a seperate folder and in that, the exel file, you have been beyond helpful so far and i can’t do enough to thank you, thankyou very much in advance