Python Forum
Advanced Calculator Python3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Advanced Calculator Python3
#1
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
Reply
#2
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 +'.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
before optimization, you may wish to add proper indentation...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Advanced CLI Snippet Manager bytebutcher 1 2,560 Sep-20-2020, 11:58 AM
Last Post: bytebutcher

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020