Skip to content

Instantly share code, notes, and snippets.

@sunsunza2009 sunsunza2009/math.py

Last active Mar 23, 2017
Embed
What would you like to do?
Math Question Generator
from random import randint
from time import sleep
def print_logo():
logo = """
_______ _______ _______ _________ _______ _______ __________________
/ ___ )|\ /|( ____ \( ____ \\__ __/( ___ )( ____ ) \__ __/\__ __/
\/ ) || ) ( || ( \/| ( \/ ) ( | ( ) || ( )| ) ( ) (
/ )| | | || | | (__ | | | | | || (____)| | | | |
/ / | | | || | ____ | __) | | | | | || __) | | | |
/ / | | | || | \_ )| ( | | | | | || (\ ( | | | |
/ (_/\| (___) || (___) || (____/\ | | | (___) || ) \ \__ ___) (___ | |
(_______/(_______)(_______)(_______/ )_( (_______)|/ \__/ \_______/ )_(
"""
for line in logo.split("\n"):
print(line)
sleep(0.025)
print_logo()
ans=int(input("Enter Answer: "))
min=int(input("Enter Min Number: "))
max=int(input("Enter Max Number: "))
print("Work Operator: + - * /")
op=input("Enter Operator: ")
Mloop=int(input("Enter Max Loop: "))-1
temp = randint(1,9)
qut = str(temp)
ansBuild = temp
run = 0
while(ans != ansBuild):
selectOp = randint(0,3)
temp = randint(min,max)
if(selectOp == 0 and "+" in op):
ansBuild = ansBuild + temp
if(temp >= 0):
qut = qut+"+"+str(temp)
else:
qut = qut+"+("+str(temp)+")"
if(selectOp == 1 and "-" in op):
ansBuild = ansBuild - temp
if(temp >= 0):
qut = qut+"-"+str(temp)
else:
qut = qut+"-("+str(temp)+")"
if(selectOp == 2 and "*" in op):
ansBuild = ansBuild * temp
if(temp >= 0):
qut = "("+qut+")"+"*"+str(temp)
else:
qut = "("+qut+")"+"*("+str(temp)+")"
if(selectOp == 3 and "/" in op and run != 0):
ansBuild = ansBuild / temp
if(temp >= 0):
qut = "("+qut+")"+"/"+str(temp)
else:
qut = "("+qut+")"+"/("+str(temp)+")"
if(run == 0):
print("Calculating...")
if(run == Mloop):
if(ansBuild < ans):
tem = ans-ansBuild
ansBuild = ansBuild+tem
qut = qut+"+"+str(tem)
else:
tem = ansBuild-ans
ansBuild = ansBuild-tem
qut = qut+"-"+str(tem)
print("Max Loop!!!")
run = run + 1
print("\nQuestion: ",qut,"\nRuntime: ",run," loop","\nAnswer: ",ans,"\nMin Number: ",min,"\nMax Number: ",max)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.