Python Forum
Advanced Calculator Python3 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: Code sharing (https://python-forum.io/forum-5.html)
+--- Thread: Advanced Calculator Python3 (/thread-5233.html)



Advanced Calculator Python3 - Gabgab2003 - Sep-23-2017

I made a Cool Calculator of a Concept ive seen a long time ago but i want to know if i can optimise something:
Code:
import os
if os.name == "nt":
cle = cls
else :
cle = "clear"
lopi = 1
while lopi == 1:
hadexception = bool(False)
bob = 0
os.system(cle)
print("Select Operation")
print("1: add")
print("2: subtract")
print("3: multiply")
print("4: divide")
print("5: x to the power of y")
print("6: divide and delete decimals")
print("7: Calculate the Factorial of a Number")
print(" Type exit to exit")
choice = input("Enter choice(1/2/3/4/5/6/7): ")
if choice == 'exit':
exit()
elif choice not in ['1', '2', '3', '4', '5', '6', '7']:
print("Operation not supported")
input("Press Enter to restart")
elif choice == '7':
try:
num1 = int(input("Enter your number: "))
except:
hadexception = bool(True)
if hadexception == bool(True):
print("Not a Valid Number")
input("Press Enter to restart")
else:
stry = num1
while bob == 0:
if not stry == 1:
stry = stry-1
num1 = num1*stry
else:
print(num1)
bob = 1
input("Press Enter to Restart")
else:
try:
num1 = int(input("Enter first number: "))
except:
hadexception = bool(True)
if hadexception == bool(True):
print("Not a Valid Number")
input("Press Enter to restart")
else:
try:
num2 = int(input("Enter second number: "))
except:
hadexception = bool(True)
if hadexception == bool(True):
print("Not a Valid Number")
input("Press Enter to restart")
else:
if choice == '1':
print(num1,'+',num2,'=',(num1+num2))
input("Press Enter to restart")
elif choice == '2':
print(num1,'-',num2,'=',(num1-num2))
input("Press Enter to restart")

elif choice == '3':
print(num1,'*',num2,'=',(num1*num2))
input("Press Enter to restart")

elif choice == '4':
print(num1,'/',num2,'=',(num1/num2))
input("Press Enter to restart")

elif choice == '5':
print(num1,'^',num2,'=',(num1**num2))
input("Press Enter to restart")

elif choice == '6':
print(num1,'//',num2,'=',(num1//num2))
print("R:", num1%num2)
input("Press Enter to restart")
else:
print("Operation not Supported")
input("Press Enter to restart")
Forgot Code Tags


RE: Advanced Calculator Python3 - ichabod801 - Sep-23-2017

What do you want to optimize?

Also, I would suggest trying to program a reverse Polish notation calculator rather than a menu driven calculator. If you are not familiar with RPN, it puts the operator at the end: '3 + 2' becomes '3 2 +'.


RE: Advanced Calculator Python3 - buran - Sep-24-2017

before optimization, you may wish to add proper indentation...