MCPcopy Create free account
hub / github.com/geekcomputers/Python / calculator

Function calculator

simple_calculator.py:38–59  ·  view source on GitHub ↗

Run a simple calculator in the console.

()

Source from the content-addressed store, hash-verified

36
37
38def calculator() -> None:
39 """Run a simple calculator in the console."""
40 print("Select operation.")
41 print("1.Add\n2.Subtract\n3.Multiply\n4.Divide")
42
43 while True:
44 choice: str = input("Enter choice (1/2/3/4): ").strip()
45 if choice in ("1", "2", "3", "4"):
46 num1: float = float(input("Enter first number: "))
47 num2: float = float(input("Enter second number: "))
48
49 if choice == "1":
50 print(f"{num1} + {num2} = {add(num1, num2)}")
51 elif choice == "2":
52 print(f"{num1} - {num2} = {subtract(num1, num2)}")
53 elif choice == "3":
54 print(f"{num1} * {num2} = {multiply(num1, num2)}")
55 elif choice == "4":
56 print(f"{num1} / {num2} = {divide(num1, num2)}")
57 break
58 else:
59 print("Invalid Input. Please select 1, 2, 3, or 4.")
60
61
62if __name__ == "__main__":

Callers 1

Calls 4

addFunction · 0.70
subtractFunction · 0.70
multiplyFunction · 0.70
divideFunction · 0.70

Tested by

no test coverage detected