▲ 4 r/PythonLearning
Just wanted to share a fun script I made.
(Note: I'm a python beginner and there might be some mistakes in the code)
What this script does is generate amount of operators that you want, in number range that you want, and then give sum answer at the end. Seems fun, right?
I don't know what new operator I'll add, I always run out of ideas, so feel free to suggest.
Here's the code below: (copy and paste then run it)
import random
import math
#vars
numOperators = 0
try:
minOperators = abs(int(input("min operators: ")))
maxOperators = abs(int(input("max operators: ")))
numberRange = abs(int(input("number range: ")))
except:
ae=input("error: the things you inputted are not numbers \n press enter to close")
quit()
answer = ""
imaginaryNUM = False
question = ""
operators = [
'+',
'-',
'*',
'/',
'^',
'cos',
'sin',
'tan',
'log',
'sqrt',
'cbrt',
'!'
]
thingsToPRINT = []
SUMS = []
iSUMS = []
#functions
def randomMath():
global operators, thingsToPRINT, SUMS, iSUMS, imaginaryNUM
selectoperator = random.choice(operators)
numFINAL = 0
#basic math
if selectoperator in ('+', '-', '*'):
num1 = random.randint(-numberRange, numberRange)
num2 = random.randint(-numberRange, numberRange)
thingsToPRINT += [str(num1), selectoperator, str(num2)]
if selectoperator == operators[0]:
numFINAL = num1 + num2
elif selectoperator == operators[1]:
numFINAL = num1 - num2
elif selectoperator == operators[2]:
numFINAL = num1 * num2
#division
elif selectoperator == '/':
num1 = random.randint(-numberRange, numberRange)
num2 = random.randint(-numberRange, numberRange)
if num2 == 0:
num2 = 1
thingsToPRINT += [str(num1), selectoperator, str(num2)]
numFINAL = num1 / num2
#exponent
elif selectoperator == '^':
num1 = random.randint(-numberRange, numberRange)
num2 = random.randint(-numberRange, numberRange)
if num1 == 0:
num1 = 1
thingsToPRINT += [str(num1), '^(', str(num2), ')']
numFINAL = num1 ** num2
#cos, sin, tan, log
elif selectoperator in ('cos', 'sin', 'tan', 'log'):
num = random.randint(-numberRange, numberRange)
SO = selectoperator
thingsToPRINT += [SO, '(', str(num), ')']
if SO == 'cos':
numFINAL = math.cos(num)
elif SO == 'sin':
numFINAL = math.sin(num)
elif SO == 'tan':
numFINAL = math.tan(num)
elif SO == 'log':
if num == 0: num = 1
numFINAL = math.log(abs(num))
if num < 0:
num = abs(num)
imnum = math.pi
iSUMS.append(imnum)
thingsToPRINT += [' = ', str(numFINAL), ' + ', str(imnum), 'i']
return
#square root
elif selectoperator == 'sqrt':
isnegative = False
num = random.randint(-numberRange, numberRange)
if num < 0: isnegative = True
if num == 0: num = 1
thingsToPRINT += ['square root of ', str(num)]
if isnegative:
imaginaryNUM = True
numFINAL = math.sqrt(abs(num))
#cube root
elif selectoperator == 'cbrt':
num = random.randint(-numberRange, numberRange)
thingsToPRINT += ['cube root of ', str(num)]
numFINAL = math.cbrt(num)
#factorial
elif selectoperator == '!':
num = random.randint(0, numberRange)
thingsToPRINT += [str(num), '!']
numFINAL = math.factorial(num)
thingsToPRINT += [' = ', str(numFINAL)]
if imaginaryNUM == True:
iSUMS.append(numFINAL)
thingsToPRINT.append('i')
else: SUMS.append(numFINAL)
imaginaryNUM = False
def printQuestion():
global question, thingsToPRINT
question = "".join(thingsToPRINT)
print(question)
def calcAnswer():
global answer, SUMS
numAns = 0
imAns = 0
for i in range(len(SUMS)):
try:
numAns += SUMS[i]
except OverflowError:
numAns = float('inf')
try:
if iSUMS[0]:
for i in range(len(iSUMS)):
imAns += iSUMS[i]
answer = f"{numAns} + {imAns}i"
return answer
except: 0
answer = f"{numAns}"
return answer
#main
if minOperators < maxOperators:
numOperators = random.randint(minOperators, maxOperators)
else:
ae=input("error: min must be smaller than max")
quit()
print()
print("operators:")
for _ in range(numOperators):
randomMath()
printQuestion()
thingsToPRINT = []
question = ""
print()
print("total answer sum:", calcAnswer())
print("number of operators used:", str(numOperators))
print()
ae=input("press enter to close")
u/windowssandbox — 1 day ago