Advertisement
Jhynjhiruu

Calculator

Sep 8th, 2017
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. import re
  2. run = True
  3. while run:
  4.     while True:
  5.         in1 = input("Please enter the first number\n")
  6.         pattern = "^[0-9]+$"
  7.         if re.fullmatch(pattern, in1):
  8.             num1 = int(in1)
  9.             break
  10.         else:
  11.             print(in1, "isn't a number! Please only enter numbers in the form 000-999 etc\n")
  12.     while True:
  13.         in2 = input("Please enter an operator\n")
  14.         pattern = "^[+ or - or / or *]"
  15.         if re.fullmatch(pattern, in2):
  16.             oper = in2
  17.             break
  18.         else:
  19.             print(in2, "isn't an operator! Please only enter +, _ (yes I know\nthat's an underscore, Python is just annoying sometimes),\n* or /\n")
  20.     while True:
  21.         in3 = input("Please enter the second number\n")
  22.         pattern = "^[0-9]+$"
  23.         if re.fullmatch(pattern, in3):
  24.             num2 = int(in3)
  25.             if num2 > 0 or oper != "/":
  26.                 break
  27.             else:
  28.                 print("You can't divide by 0! Input a different number\n")
  29.         else:
  30.             print(in3, "isn't a number! Please only enter numbers in the form 000-999 etc\n")
  31.     if oper == "+":
  32.         output = num1 + num2
  33.     elif oper == "-":
  34.         output = num1 - num2
  35.         oper = "-"
  36.     elif oper == "*":
  37.         output = num1 * num2
  38.     else:
  39.         output = num1 / num2  
  40.     print(num1, oper, num2, "=", output)
  41.     while True:
  42.         restart = input("Enter r to restart or s to stop\n")
  43.         pattern = "^[a-z]*$"
  44.         if re.fullmatch(pattern, restart) and len(restart) < 2:
  45.             if restart == "s":
  46.                 run = False
  47.                 break
  48.             if restart == "r":
  49.                 print("----------Restarting----------")
  50.                 break            
  51.         else:
  52.             print(restart, "isn't r or s, is it?\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement