Tiny calculator on Python 3
Hi, wrote some code, please advice how this idea can be optimized (less code, DRY). I'm just studing the Python, so know very little...) import re while True: while True: i = input('1st number: ') pattern = r"[0-9]" if re.search(pattern, i): break else: print('Only numbers allowed. ') while True: j = input('Choose + - * / as operator: ') if j in ['+', '-', '*', '/']: break else: print('Invalid operator. Try + - * / ') while True: k = input('2nd number: ') pattern = r"[0-9]" if re.search(pattern, k): if k == '0' and j == '/': print('No division by zero. ') else: break else: print('Only numbers allowed. ') d = eval(i + j + k) print('Result:', d, '\n') print('=' * 7)