Add power support

This commit is contained in:
cheetosysst 2019-11-10 14:16:38 +08:00
parent a7aabeaf49
commit 88f7a620e8
2 changed files with 5218 additions and 6 deletions

View File

@ -1,5 +1,6 @@
from decimal import Decimal as d
import decimal
import math
# Generator used to create my_first_calculator
# Open a file that we can write to
@ -8,7 +9,7 @@ python_file = open('my_first_calculator.py', 'w')
min_num = 0
max_num = 50
nums = range(min_num, max_num+1)
signs = ['+', '-', '/', '*']
signs = ['+', '-', '/', '*', 'p']
num_of_ifs = len(signs)*(max_num-min_num+1)**2
print("""# my_first_calculator.py by AceLewis
@ -20,7 +21,7 @@ if 3/2 == 1: # Because Python 2 does not know maths
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 *: ')
sign = input('What do you want to do? +, -, /, *, or p: ')
num2 = int(input('Please choose your second number: '))
""".format(min_num, max_num), file=python_file)
@ -28,9 +29,15 @@ num2 = int(input('Please choose your second number: '))
for sign in signs:
for num1 in nums:
for num2 in nums:
equation = "d({}){}d({})".format(num1, sign, num2)
if sign == 'p':
equation = ""
else:
equation = "d({}){}d({})".format(num1, sign, num2)
try:
equals = eval(equation)
if sign == 'p':
equals = math.pow(num1,num2)
else:
equals = eval(equation)
except ZeroDivisionError:
equals = 'Inf'
except decimal.InvalidOperation as error:
@ -41,7 +48,10 @@ for sign in signs:
# 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)
if sign is "p":
print(' print("{} power {} = {}")'.format(num1, num2, equals), file=python_file)
else:
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)

File diff suppressed because it is too large Load Diff