Advertisement
kingbode

Untitled

Sep 19th, 2023
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 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 in operators:
  36.     print(f"{int(num1)} {operator} {int(num2)} = {eval(str(num1)+operator+str(num2))}")
  37. else:
  38.     print("invalid operator")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement