Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # store all possible operators
- operators = ["+","-","*","/","**","//"]
- validNumber = False
- while not validNumber:
- try:
- num1 = float(input("enter the first number : "))
- validNumber = True
- except ValueError:
- print("invalid number")
- validOperator = False
- while not validOperator:
- try:
- operator = input("enter the operator \n(+ or - or * or / or ** = Power or // ) : ")
- if operator in operators:
- validOperator = True
- else:
- print("invalid operator")
- except ValueError:
- print("invalid operator")
- validNumber = False
- while not validNumber:
- try:
- num2 = float(input("enter the second number : "))
- validNumber = True
- except ValueError:
- print("invalid number")
- print()
- if operator == "+":
- print(f"{int(num1)} + {int(num2)} = {num1+num2}")
- elif operator == "-":
- print(f"{int(num1)} {int(num2)} = {num1-num2}")
- elif operator == "*":
- print(f"{int(num1)} x {int(num2)} = {num1*num2}")
- elif operator == "x":
- print(f"{int(num1)} x {int(num2)} = {num1*num2}")
- elif operator == "/":
- print(f"{int(num1)} / {int(num2)} = {num1/num2}")
- elif operator == "+":
- print(f"{int(num1)} + {int(num2)} = {num1/num2}")
- elif operator == "**":
- print(f"{int(num1)} ** {int(num2)} = {num1**num2}")
- elif operator == "//":
- print(f"{int(num1)} // {int(num2)} = {num1//num2}")
- else:
- print("you used invalid operator.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement