First commit

This commit is contained in:
AceLewis 2016-05-06 18:48:18 +01:00
parent f8a9aace4d
commit 5b9ef48f1f
4 changed files with 20877 additions and 0 deletions

11
README.md Normal file
View File

@ -0,0 +1,11 @@
# my_first_calculator.py
I initialls saw [Al Sweigart](https://github.com/asweigart)'s [my_first_tic_tac_toe](https://github.com/asweigart/my_first_tic_tac_toe) and was amused. I then saw the image shown below posted on Reddit. Assuming that the image was not made up for some fake internet points and also assuming that their friend only used addition, subtraction, multiplication and division then the number of numbers that they would have if statements for would be...
sqrt(9500/4) = 48.7339...
sqrt(9500/4) ≈ 50
So to be true to the "real" story I have only gone from 0-50 however higher numbers can easily be generated too however my Python crashes with larger numbers. I generated one that was 0-1000 and it took up 317 MB of space on my hard drive but was only 20MB after I compressed it to a .rat so I have also attatched it.
The generator will not work in Python 2 it can easily be patched to work by putting a ".0" on the end of the eval and then cutting off the last two when printing the equation but I decided to not do what because it would add a ".0" to the end of additions, subtractions and multiplications (or you could use an if statement but I decided to miss that out).
![The image](https://i.imgur.com/ZMvUovj.png)

44
generator.py Normal file
View File

@ -0,0 +1,44 @@
# 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)**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 = "{}{}{}".format(num1, sign, num2)
try:
equals = eval(equation)
except ZeroDivisionError:
equals = 'Inf'
# 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(equation, 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()

20822
my_first_calculator.py Normal file

File diff suppressed because it is too large Load Diff

Binary file not shown.