forked from AceLewis/my_first_calculator.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
50 lines (43 loc) · 1.87 KB
/
generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from decimal import Decimal as d
import decimal
# Generator used to create my_first_calculator
# Open a file that we can write to
python_file = open('my_first_calculator.py', 'w')
# The minimum and maximum numbers we can use
min_num = 0
max_num = 50
nums = range(min_num, max_num+1)
signs = ['+', '-', '/', '*']
num_of_ifs = len(signs)*(max_num-min_num+1)**2
print("""# my_first_calculator.py by AceLewis
# TODO: Make it work for all floating point numbers too
if 3/2 == 1: # Because Python 2 does not know maths
input = raw_input # Python 2 compatibility
print('Welcome to this calculator!')
print('It can add, subtract, multiply and divide whole numbers from {} to {}')
num1 = int(input('Please choose your first number: '))
sign = input('What do you want to do? +, -, /, or *: ')
num2 = int(input('Please choose your second number: '))
""".format(min_num, max_num), file=python_file)
# For all the numbers and all the
for sign in signs:
for num1 in nums:
for num2 in nums:
equation = "d({}){}d({})".format(num1, sign, num2)
try:
equals = eval(equation)
except ZeroDivisionError:
equals = 'Inf'
except decimal.InvalidOperation as error:
if error == decimal.DivisionByZero:
equals = 'Inf'
else:
equals = 'Undefined'
# No elif's used to be true to the story and also because
# Python will throw a recursion error when too many are used
print("if num1 == {} and sign == '{}' and num2 == {}:".format(num1, sign, num2), file=python_file)
print(' print("{}{}{} = {}")'.format(num1, sign, num2, equals), file=python_file)
print('', file=python_file)
print('print("Thanks for using this calculator, goodbye :)")', file=python_file)
# Close the file we have written to
python_file.close()