Advertisement
kingbode

Untitled

Sep 19th, 2023 (edited)
861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1.  
  2. # store all possible operators
  3. operators = ["+","-","*","/","**","//"]
  4.  
  5. validNumber = False
  6. while not validNumber:
  7.     try:
  8.         num1 = float(input("enter the first number : "))
  9.         validNumber = True
  10.     except ValueError:
  11.         print("invalid number")
  12.  
  13.  
  14. validOperator = False
  15. while not validOperator:
  16.     try:
  17.         operator = input("enter the operator \n(+ or - or * or / or ** = Power or // ) : ")
  18.         if operator in operators:
  19.             validOperator = True
  20.         else:
  21.             print("invalid operator")
  22.     except ValueError:
  23.         print("invalid operator")
  24.  
  25. validNumber = False
  26. while not validNumber:
  27.     try:
  28.         num2 = float(input("enter the second number : "))
  29.         validNumber = True
  30.     except ValueError:
  31.         print("invalid number")
  32.  
  33. print()
  34.  
  35. if operator == "+":
  36.     print(f"{int(num1)} + {int(num2)} = {num1+num2}")
  37. elif operator == "-":
  38.     print(f"{int(num1)} {int(num2)} = {num1-num2}")
  39. elif operator == "*":
  40.     print(f"{int(num1)} x {int(num2)} = {num1*num2}")
  41. elif operator == "x":
  42.     print(f"{int(num1)} x {int(num2)} = {num1*num2}")
  43. elif operator == "/":
  44.     print(f"{int(num1)} / {int(num2)} = {num1/num2}")
  45. elif operator == "+":
  46.     print(f"{int(num1)} + {int(num2)} = {num1/num2}")
  47. elif operator == "**":
  48.     print(f"{int(num1)} ** {int(num2)} = {num1**num2}")
  49. elif operator == "//":
  50.     print(f"{int(num1)} // {int(num2)} = {num1//num2}")
  51. else:
  52.     print("you used invalid operator.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement