Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Adam Siwiec
12,070 PointsRefactoring in Python, Scope Help Please!
Hey guys! I need help refactoring this code. It seems very repetitive and I want it to be DRY. If I were to split some things up into functions, then I would have a lot of issues with scope to deal with, especially with the timer. Any tips?
import time
import datetime
import random
from matplotlib import pyplot as plt
import numpy as np
timeList = []
questionNumber = []
exit = 0
timeBeginning = time.time()
timeEnd = time.time()
n = 0
def endTime():
timeEnd = time.time()
timeLapse = timeEnd - timeBeginning
timeList.append(int(timeLapse))
def stats():
totalCorrect = add_quiz() + subtract_quiz() + multiply_quiz() + divide_quiz()
print("Total Correct: " + str(totalCorrect))
print("Total Time Per Each Question: " + str(timeList))
questionNumber = list(range(1,totalCorrect + 1) )
x = questionNumber
y = timeList
print(x)
print(y)
plt.plot(x,y, color="red", marker="o", linestyle ='solid')
plt.title("Time spent Per Question")
plt.show()
def add_quiz():
n = 0
additionAnswers = 0
while True:
n += 1
maxnum = 2 ** n
num1 = random.randint(1,maxnum)
num2 = random.randint(1,maxnum)
print ("{} + {}".format(num1,num2))
timeBeginning = time.time()
answer = int(input(""))
if (answer == num1 + num2):
timeEnd = time.time()
timeLapse = timeEnd - timeBeginning
timeList.append(int(timeLapse))
additionAnswers += 1
continue
else:
return additionAnswers
def subtract_quiz():
n = 0
subtractionAnswers = 0
while True:
n += 1
maxnum = 2 ** n
num1 = random.randint(1,maxnum)
num2 = random.randint(1,maxnum)
print ("{} - {}".format(num1,num2))
timeBeginning = time.time()
answer = int(input(""))
if (answer == num1 - num2):
timeEnd = time.time()
timeLapse = timeEnd - timeBeginning
timeList.append(int(timeLapse))
subtractionAnswers += 1
continue
else:
return subtractionAnswers
def multiply_quiz():
n = 0
multiplicationAnswers = 0
while True:
n += 1
maxnum = 2 ** n
num1 = random.randint(1,maxnum)
num2 = random.randint(1,maxnum)
print ("{} x {}".format(num1,num2))
timeBeginning = time.time()
answer = int(input(""))
if (answer == num1 * num2):
timeEnd = time.time()
timeLapse = timeEnd - timeBeginning
timeList.append(int(timeLapse))
multiplicationAnswers += 1
continue
else:
return multiplicationAnswers
def divide_quiz():
n = 0
divisionAnswers = 0
while True:
n += 1
maxnum = 2 ** n
num1 = random.randint(1,maxnum)
num2 = random.randint(1,maxnum)
print ("{} / {}".format(num1,num2))
timeBeginning = time.time()
answer = int(input(""))
if (answer == num1 // num2):
timeEnd = time.time()
timeLapse = timeEnd - timeBeginning
timeList.append(int(timeLapse))
divisionAnswers += 1
continue
else:
return divisionAnswers
def graph():
pass
def take_quiz():
print("Welcome to Adam's Math Indexing Quiz")
print("This Quiz will help you evaluate your math skills and speed!")
print("Remember: NO CALCULATORS/PAPER!")
print("Remember: Division is to the nearest whole number (i.e. 10 / 3 = 3, 14 / 3 = 4)")
response = input("Press enter to start")
if not response:
stats()
post()
graph()
take_quiz()
ianmckellar
4,344 Pointsianmckellar
4,344 PointsHi Adam, I know this isn't what you're looking for but how did you get matplotlib.pyplot into your code? Can you get it in workspaces? - Ian