calculator.py 855 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. def add(a, b):
  2. return a + b
  3. def subtract(a, b):
  4. return a - b
  5. def multiply(a, b):
  6. return a * b
  7. def divide(a, b):
  8. return a / b
  9. operations = {
  10. "+": add,
  11. "-": subtract,
  12. "*": multiply,
  13. "/": divide
  14. }
  15. def compute():
  16. n1 = float(input("What's the first number?: "))
  17. for sign in operations:
  18. print(sign)
  19. while True:
  20. operation_sign = input("Pick an operation sign from the line above: ")
  21. n2 = float(input("What's the next number?: "))
  22. result = operations[operation_sign](n1, n2)
  23. print(f"{n1} {operation_sign} {n2} = {result}")
  24. n1 = result
  25. if input(f"If you want another operation with {n1} type 'y', for start calculation from begining type 'c' and type 'n' for stop calc: ") == "c":
  26. break
  27. else:
  28. compute()
  29. print("Bye, bye!")