Python Maths Quiz- Outputting Correct or Incorrect

Question:

import random
import time
counter=0
score=0
count=0

function=['+','x','÷','-']

print('Welcome To The Arithmetic Quiz!')
name=input('Please enter you name..')
print('Thanks' , name , 'Lets Get Started!')

while counter <10:
    firstnumber=random.randint(0,12)
    secondnumber=random.randint(0,6)
    function=random.choice(function)

question=print(firstnumber, function, secondnumber, '=')
input('Answer:')
counter= counter+1

if function== '+':
                count==firstnumber+secondnumber
                if count == int (answer):
                    print ('Correct!')
                    score= score+1
                else:
                    print ('Incorrect')
elif function== 'x':
                count==firstnumber*secondnumber
                if count == int (answer):
                    print ('Correct!')
                    score= score+1
                else:
                    print ('Incorrect')
elif function== '-':
                count==firstnumber-secondnumber
                if count == int (answer):
                    print ('Correct!')
                    score= score+1
                else:
                    print ('Incorrect')
elif function== '÷':
                count==firstnumber/secondnumber
                if count == int (answer):
                    print ('Correct!')
                    score= score+1
                else:
                    print ('Incorrect')

Could anybody correct the end section ( if function ) and ( elif function )
I think it is something to do with the variable names.

It also does not run properly as it stops at: print('Thanks' , name , 'Lets Get Started!'), again I am unsure why this is.

Asked By: user4593673

||

Answers:

Your counter isn’t incrementing within your while loop, as you have some serious indentation problems. As a result, your while loop just runs forever and you have an infinite loop. Make sure you indent the code including the counter which you wish to have in your while loop in order for that code to be executed and for your while loop to actually stop.

EDIT:
I fixed your indentation and your counter. Note that your division still will not work unless the quotient happens to be an integer, if you want to fix that you’ll have to do some research yourself.

import random
import time
counter=0
score=0
count=0

function=['+','x','÷','-']

print('Welcome To The Arithmetic Quiz!')
name=input('Please enter you name.')
print('Thanks' , name , 'Lets Get Started!')

while counter <10:
    firstnumber=random.randint(0,12)
    secondnumber=random.randint(0,6)
    operator=random.choice(function)

    question=print(firstnumber, operator, secondnumber, '=')
    userAnswer = input('Answer:')


    if operator== '+':
                    count=firstnumber+secondnumber
                    if count == int (userAnswer):
                        print ('Correct!')
                        score= score+1
                    else:
                        print ('Incorrect')
    elif operator== 'x':
                    count=firstnumber*secondnumber
                    if count == int (userAnswer):
                        print ('Correct!')
                        score= score+1
                    else:
                        print ('Incorrect')
    elif operator== '-':
                    count=firstnumber-secondnumber
                    if count == int (userAnswer):
                        print ('Correct!')
                        score= score+1
                    else:
                        print ('Incorrect')
    elif operator== '÷':
                    count=firstnumber/secondnumber
                    if count == int (userAnswer):
                        print ('Correct!')
                        score= score+1
                    else:
                        print ('Incorrect')
    counter += 1

print ("Your quiz is over!")
Answered By: HavelTheGreat

Your code is poorly indented, as Elizion has pointed out. Also count==number + number should be count=number + number.
Then function is assigned a value which can never be changed – consider changing one of the variable names. Use else, not elif for the final possible condition. How about printing the score at the end.
I’ve corrected your code, but have a go yourself first…

Answered By: BendyMan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.