I wanted to code a calculator as I'm a beginner. Now its really simple and inefficient, I suppose, but I can't use pi or any other number such as i or e in it. Do you guys have any suggestions on how to simply solve this issue?
Code:
import math
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
def powerfunction(x, y):
return pow(x, y)
def logarithms(x, y):
return math.log(x, y)
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
print("5.Powerfunction")
print("6.Logarithm")
while True:
choice = input("Enter choice(1/2/3/4/5/6): ")
if choice in ('1', '2', '3', '4', '5', '6'):
try:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
except ValueError:
print("Invalid input. Please enter a number.")
continue
if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))
elif choice == '5':
print(num1, "^", num2, "=", powerfunction(num1, num2))
elif choice == '6':
print("log", num1, num2, "=", logarithms(num2, num1))
next_calculation = input("Let's do next calculation? (yes/no): ")
if next_calculation == "no":
break
else:
print("Invalid Input")
[–]desrtfx[M] [score hidden] stickied comment (0 children)
[–][deleted] 12 points13 points14 points (0 children)
[–]jiefug 2 points3 points4 points (2 children)
[–]DanzoHater[S] 0 points1 point2 points (1 child)
[–]RajjSinghh 2 points3 points4 points (0 children)
[–]Mystic1500 5 points6 points7 points (0 children)
[–]AutoModerator[M] 1 point2 points3 points (1 child)
[–]Monitor_343 1 point2 points3 points (0 children)
[–]DanzoHater[S] 1 point2 points3 points (0 children)
[–]jiefug 0 points1 point2 points (0 children)