Created
April 29, 2019 14:51
-
-
Save JoshOrndorff/96534038791d08a4f7c4cd093d98432e to your computer and use it in GitHub Desktop.
Arithmetic Quiz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Arithmetic Quiz | |
# Author: Julie Coxe | |
# 24 April 2019 | |
# Quiz a student in single-step addition, subtraction, multiplication, and division problems. | |
# Division answers should be the quotient only, no matter the remainder. | |
import random | |
# question construction functions | |
def newquestion(): | |
first = random.randint(0, 10) | |
second = random.randint(0, 10) | |
operation = random.randint(1, 5) | |
question = "" | |
answer = 0 | |
# addition question | |
if operation == 1: | |
question, answer = addition(first, second) | |
# subtraction question | |
elif operation == 2: | |
question, answer = subtraction(first, second) | |
# multiplication question | |
elif operation == 3: | |
question, answer = multiplication(first, second) | |
# division question. Use integer division. Larger number must be "on top." | |
elif operation == 4: | |
question, answer = division(first, second) | |
return question, answer | |
# calculation functions below for add, sub, mult, and int div | |
def addition(first, second): | |
answer = first + second | |
question = str(first) + " + " + str(second) + " = " | |
return question, answer | |
def subtraction(first, second): | |
answer = first - second | |
question = str(first) + " - " + str(second) + " = " | |
return question, answer | |
def multiplication(first, second): | |
answer = first * second | |
question = str(second) + " * " + str(first) + " = " | |
return question, answer | |
def division(first, second): | |
if first == 0: | |
answer = first // second | |
question = str(first) + " / " + str(second) + " = " | |
elif second ==0: | |
answer = second // first | |
question = str(second) + " / " + str(first) + " = " | |
elif first >= second: | |
answer = first // second | |
question = str(first) + " / " + str(second) + " = " | |
else: | |
answer = second // first | |
question = str(second) + " / " + str(first) + " = " | |
return question, answer | |
# main program for executing quiz: | |
userAnswer = 0 | |
isItQ = 0 | |
print("Welcome to your arithmetic quiz! Here we go!") | |
while isItQ != 1: | |
# call question generator | |
question, answer = newquestion() | |
gameTime = 0 | |
while gameTime != 1: | |
# check to make sure userAnswer is an integer | |
try: | |
# change type of userAnswer | |
userAnswer = input(question) | |
userAnswer = int(userAnswer) | |
# check if the answer is correct. | |
if int(userAnswer) == int(answer): | |
print("Correct!") | |
gameTime = 1 | |
# if the answer is not right, ask it again until it is correct. | |
elif int(userAnswer) != int(answer): | |
print("Nope, try again.") | |
userAnswer = input(question) | |
# if letters given, quit the quiz if "q" is given. | |
except ValueError: | |
print(userAnswer) | |
# quit | |
if str(userAnswer) == "q": | |
isItQ = 1 | |
gameTime = 1 | |
# reenter the loop asking for a new answer. Same problem given. | |
else: | |
print("Not a number. Please answer the question with an integer. ") | |
userAnswer = input(question) | |
# exit quiz with a "q." | |
print("Great job! Come back soon! ") |
This SO will probably piss you off ;) but also probably help https://stackoverflow.com/questions/2568783/python-why-does-random-randinta-b-return-a-range-inclusive-of-b
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I figured out why the question is sometimes set to empty string. Hint: Add
print("operation is {}".format(operation))
on line 16.