Fixed 0/0 being Inf not undefined
I also made the prints fit on one line. Although the line length is larger than it should be I don't think it matter too much as modern monitors can easily display lines that long and it is much easier to read.
This commit is contained in:
parent
d1fd350e83
commit
1d300c065e
19
generator.py
19
generator.py
|
|
@ -14,9 +14,9 @@ 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
|
||||
|
||||
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: '))
|
||||
|
|
@ -33,14 +33,15 @@ for sign in signs:
|
|||
equals = eval(equation)
|
||||
except ZeroDivisionError:
|
||||
equals = 'Inf'
|
||||
except decimal.InvalidOperation:
|
||||
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("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)
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
# 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
|
||||
|
||||
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 0 to 50')
|
||||
num1 = int(input('Please choose your first number: '))
|
||||
|
|
@ -10415,7 +10415,7 @@ if num1 == 50 and sign == '-' and num2 == 49:
|
|||
if num1 == 50 and sign == '-' and num2 == 50:
|
||||
print("50-50 = 0")
|
||||
if num1 == 0 and sign == '/' and num2 == 0:
|
||||
print("0/0 = Inf")
|
||||
print("0/0 = Undefined")
|
||||
if num1 == 0 and sign == '/' and num2 == 1:
|
||||
print("0/1 = 0")
|
||||
if num1 == 0 and sign == '/' and num2 == 2:
|
||||
|
|
|
|||
Loading…
Reference in New Issue