1
Good morning/late/evening. I am trying to create a simple calculator in python in the IDE Visual studio. The following is the script (sorry to be in English):
import os,sys
#from colored import fg,bg,attr
print("\n\n==>This is the calculator example.")
oneInp = False
def clear():
os.system('cls')
def calc_simple(n1=0.0, n2=0.0, op=""):
if n1 == float(n1) and n2 == float(n2):
if op == "+":
return float(n1 + n2)
elif op == "-":
return float(n1 - n2)
elif op == "*":
return float(n1 * n2)
elif op == "/":
return float(n1 / n2)
elif op == "**":
return float(n1 ** n2)
elif op == "//":
return nth_root(n1, n2)
elif op == "%":
return float(n1 * (n2 / 100))
else:
return "Please type an option..."
else:
return "Error"
def nth_root(val=0.0, n=0.0):
ret = val ** (1 / n)
return ret
def confirmation():
confirm = input(">>Are you sure (y/n): ")
if confirm == "y":
sys.exit()
elif confirm == "n":
clear()
maincode()
else:
print("\n==> Error! \n==> Cause is: " + "The typed value isn't valid." + "\n")
confirmation()
def shapecheck(inp):
if inp == "ret":
return "base"
elif inp == "sqr":
return "side"
elif inp == "par":
return "base"
elif inp == "tri":
return "base"
elif inp == "tra":
return "bigger base"
elif inp == "cir":
return "radius"
def shapecalc(inp, n1, n2, n3):
if inp == "ret":
return n1 * n2
elif inp == "sqr":
return n1**2
elif inp == "par":
return n1*n2
elif inp == "tri":
return (n1*n2)/2
elif inp == "tra":
return ((n1+n3)*n2)/2
elif inp == "cir":
return n1*3.14
'''def config():
c1 = np.array(["EnableInt", "Unnecessary"])
c2 = np.array(["True", ""])
data = np.column_stack((c1, c2))
np.savetxt("cfg.cf", data, delimiter=":", fmt="%s")
txt1 = np.genfromtxt("cfg.cf", delimiter=":", usecols=(0), dtype=None, names=['c1'])
txt2 = np.genfromtxt("cfg.cf", delimiter=":", usecols=(1), dtype=None, names=['c2'])
cfg1 = txt2[1]
cfg1 = np.array_str(cfg1)
print(cfg1)
cfg1 = bool(cfg1)
print(cfg1)
if cfg1 == True:
print("Advanced activated!")
else:
print("Advanced unactivated!")
'''
def maincode():
oneInp = False
try:
cmd = input("\n>>Choose a command (calc, area, reset, close):")
if cmd == "calc":
clear()
print("------|Calc|------------------\n")
n1 = float(input("\n>>Type the first operation number:"))
operation = input(">>Select operation to do (+, -, *, /, **, //, %), without any spaces:")
op_txt = ""
if operation == "+":
op_txt = "second term"
elif operation == "-":
op_txt = "subtrahend"
elif operation == "*":
op_txt = "factor"
elif operation == "/":
op_txt = "divisor"
elif operation == "**":
op_txt = "expoent"
elif operation == "//":
op_txt = "root index"
elif operation == "%":
op_txt = "percentage"
n2 = float(input(">>Type the {0}:".format(op_txt)))
clear()
print("\n-------Result-------\n->%6.2f\n--------------------" % (calc_simple(n1, n2, operation)))
elif cmd == "area":
clear()
print("------|Area|------------------\n")
oneInp = False
measure = ""
measure_tra = ""
shape = input(">>Select the shape (ret, sqr, par, tri, tra, cir):")
n1 = 0
n2 = 0
n3 = 0
if shape == "tra":
measure_tra = "smaller base"
elif shape == "sqr" or shape == "cir":
oneInp = True
else:
measure_tra = "height"
measure = shapecheck(shape)
n1 = float(input(">>Type the {0}:".format(measure)))
if oneInp == False:
n2 = float(input(">>Type the height:"))
if shape == "tra":
n3 = float(input(">>Type the {0}:".format(measure_tra)))
clear()
print("\nArea: {0}".format(shapecalc(shape,n1,n2,n3)))
elif cmd == "reset":
clear()
print("------|Reset|------------------\n")
reset_app = sys.executable
clear()
os.execl(reset_app, reset_app, *sys.argv)
elif cmd == "close":
clear()
print("------|Close|------------------\n")
confirmation()
#elif cmd == "config":
#config()
#print("\n==>W.I.P.")
else:
print(
"\n==> Error! \n==> Cause is: " + "The typed option doesn't exists." +
"\n==> Restarting the script...\n")
maincode()
except WindowsError:
print("z")
except ValueError:
print("\n==> Error! \n==> Cause is: " + "The typed value isn't valid." + "\n==> Restarting the script...\n")
maincode()
except TypeError:
print("\n==> Error! \n==> Cause is: " + "There isn't any results." + "\n==> Restarting the script...\n")
maincode()
maincode()
maincode()
However, when executing the code, I get the following error:
TypeError: source code string cannot contain null bytes
Why am I receiving it? Could you help me?
Which line of error?
– Jéf Bueno
unfortunately only gives me as line 1, before received errors related to coding(utf-8, ansi, ascii...)
– Pedro Brito