Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- run = True
- while run:
- while True:
- in1 = input("Please enter the first number\n")
- pattern = "^[0-9]+$"
- if re.fullmatch(pattern, in1):
- num1 = int(in1)
- break
- else:
- print(in1, "isn't a number! Please only enter numbers in the form 000-999 etc\n")
- while True:
- in2 = input("Please enter an operator\n")
- pattern = "^[+ or - or / or *]"
- if re.fullmatch(pattern, in2):
- oper = in2
- break
- else:
- print(in2, "isn't an operator! Please only enter +, _ (yes I know\nthat's an underscore, Python is just annoying sometimes),\n* or /\n")
- while True:
- in3 = input("Please enter the second number\n")
- pattern = "^[0-9]+$"
- if re.fullmatch(pattern, in3):
- num2 = int(in3)
- if num2 > 0 or oper != "/":
- break
- else:
- print("You can't divide by 0! Input a different number\n")
- else:
- print(in3, "isn't a number! Please only enter numbers in the form 000-999 etc\n")
- if oper == "+":
- output = num1 + num2
- elif oper == "-":
- output = num1 - num2
- oper = "-"
- elif oper == "*":
- output = num1 * num2
- else:
- output = num1 / num2
- print(num1, oper, num2, "=", output)
- while True:
- restart = input("Enter r to restart or s to stop\n")
- pattern = "^[a-z]*$"
- if re.fullmatch(pattern, restart) and len(restart) < 2:
- if restart == "s":
- run = False
- break
- if restart == "r":
- print("----------Restarting----------")
- break
- else:
- print(restart, "isn't r or s, is it?\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement