This is the code:
print ("____________________________")
print ("Advanced calculator selected")
print ("Experimental! May break.")
print ("NOTE: Does not work with variables... yet.")
print ("Key:", "\"()\" mean parenthesis", "\"^\" mean exponent", "\"*\" means multiplication", "\"/\" means division", "\"+\" means addition", "\"-\" means subtraction", sep="\n ")
option = "3(5+3)" #input("Equation to solve:").strip()
option2 = list(option)
count = -1
c = True
while count < len(option2) - 1:
if c is True:
count += 1
c = False
elif c is False:
c = True
if option2[count] == " ":
option2.pop(count)
count -= 1
continue
elif option2[count] in [0,1,2,3,4,5,6,7,8,9] and option2[count + 1] == "(":
option2.insert(count + 1, "*")
count += 1
elif option2[count] in ["(", ")", "*", "/", "+", "-"]:
continue
elif option2[count] == "^":
option2.pop(count)
option2.insert(count, "**")
elif str(option2[count]).isalpha() is True:
print ("Option not recognized, restarting")
counting.Calculator.Advanced()
else:
continue
print (option2)
answer = "".join(option2)
print (answer.isascii())
answer = eval(str(answer))
print (answer)
input()
I can't figure out why it's saying this. It's supposed to just spit out that equation in option
as a number but it's instead giving an error.
I have tried using str()
, I've tried map()
. I have 0 clue what is wrong, ChatGPT didn't help either, and neither did documentation on the function.
Every element of option2
is a string. That means this statement will never be true:
elif option2[count] in [0,1,2,3,4,5,6,7,8,9] and option2[count + 1] == "(":
option2.insert(count + 1, "*")
count += 1
Instead, you want to know if the string contains ASCII digits:
elif option2[count].isdigit() and option2[count + 1] == "(":
option2.insert(count + 1, "*")
count += 1
I don't know what your c
flag is trying to, but it seems pointless. Why don't you just do the more Pythonic way:
for char in option:
You can build a new string to be evaled, if you need to, although eval
is inherently dangerous.