Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 5 - Loops - Programming Exercises - Page 159: 5.2

Answer

This is an example of the code explained below; it will generate 10 random addition questions. What is 5+1?

Work Step by Step

import random import time correctCount = 0 count = 0 NUMBER_OF_QUESTIONS = 10 startTime = time.time() while count < NUMBER_OF_QUESTIONS: number1 = random.randint(0,9) number2 = random.randint(0,9) if number1 < number2: number1, number2 = number2, number1 answer = eval(input("What is " +str(number1) + "+" +str(number2) + "? ")) if number1 + number2 == answer: print("You are correct!") correctCount += 1 else: print("Your answer is wrong.\n",number1, "+",number2, "is", number1 + number2) count += 1 endTime = time.time() testTime = int(endTime -startTime) print("Correct count is", correctCount, "out of", NUMBER_OF_QUESTIONS, "\nTest time is", testTime, "seconds")
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.