Demo entry 3889888

1

   

Submitted by anonymous on Mar 05, 2016 at 14:27
Language: Python. Code size: 3.8 kB.

import random

#operator array
operators = ['+','-','*']

#names array
names = ['Annabelle', 'Fred', 'James', 'Harry', 
'Isabel', 'Ella', 'Molly', 'Jessica', 'Ben', 'Luke']

#----------------------------------------------------------------------
# Global variables
#----------------------------------------------------------------------

#variable to store the number of questions asked
questionsasked=0

#variable to store the number of questions that the student has answered correct
score=0

#variable to store the answer that the student enters to each question
useranswer=0

#variable to store the correct answer to each question
answer=0

#----------------------------------------------------------------------
# This function asks the user to enter a number and then checks that
# the value entered is really a number. The user must keep entering
# values until an integer is entered which is then returned.
def accept_number():
    input_valid = 0
    num = 0
    while (input_valid == 0):
        try:
            num = int(raw_input())
            input_valid = 1
        except ValueError:
            print "Your answer must be a whole number! Please try again"
    return num

#----------------------------------------------------------------------
# This function checks that the answer that the user has inputted is correct
def check_answer(useranswer, answer):
    global score
    if useranswer == answer:
        print "Correct"
        #increase user's score
        score=score+1
    else:
        print "Incorrect. The correct answer was " + str(answer)

#----------------------------------------------------------------------
# Main program starts here
#----------------------------------------------------------------------

name=raw_input("What is your name?")

#while loop to validate the name that the student inputs
while name not in names:
    print ('That name is not recognised, please try again')
    #if the name that the student enters is invalid, then the 
    #program will continue iterating until a valid name is entered
    name=raw_input("What is your name?")
    
print ('Welcome to this arithmetic quiz. You will now be asked 10 questions. Please enter an answer for each question.')

while questionsasked<10:
    #generate two random numbers between 1 and 15
    no1=random.randint(1,15)
    no2=random.randint(1,15)
    #if stements to ensure that no1 is greater than no2, so subtraction
    #will not result in a negative answer
    if no1<no2:
        tempnum=no1
        no1=no2
        no2=tempnum
    #pick a random operator from the operators array
    operator=random.choice(operators)

    if operator == '+':
        #output question to user
        print "What is " + str(no1) + "+" + str(no2)
        #calculate correct answer
        answer=no1+no2
        #call accept_number function to validate user answer
        useranswer=accept_number()
    elif operator == '-':
        #output question to user
        print "What is " + str(no1) + "-" + str(no2)
        #calculate correct answer
        answer=no1-no2
        #call accept_number function to validate user answer
        useranswer=accept_number()
    elif operator == '*':
        #output question to user
        print "What is " + str(no1) + "*" + str(no2)        
        #calculate correct answer
        answer=no1*no2
        #call accept_number function to validate user answer
        useranswer=accept_number()
        
    #call check_answer function
    check_answer(useranswer, answer)
    #increment number of questionsasked
    questionsasked=questionsasked+1

# Output user's final score
print "Your score was " + str(score)

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).