moved the import command to the top of the file and added docstrings to the methods
This commit is contained in:
parent
5cd80e5354
commit
19035ab84f
@ -1,12 +1,22 @@
|
|||||||
|
from math import log
|
||||||
|
|
||||||
class Calculator():
|
class Calculator():
|
||||||
from math import log
|
"""Calculator class for the Intermediate Python
|
||||||
|
class led by Ed Fine from Pluralsight. This has
|
||||||
|
basic calculator functions including keeping a running
|
||||||
|
total.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.total = 0.0
|
self.total = 0.0
|
||||||
self.actions = []
|
self.actions = []
|
||||||
|
|
||||||
def add(self, val1, val2=None):
|
def add(self, val1, val2=None):
|
||||||
|
"""Add two numbers together and prints the result
|
||||||
|
then adds the sum to the running total. With only
|
||||||
|
one number provided, just add that to the running
|
||||||
|
total.
|
||||||
|
"""
|
||||||
running_total = self.total
|
running_total = self.total
|
||||||
|
|
||||||
if val2:
|
if val2:
|
||||||
@ -21,6 +31,11 @@ class Calculator():
|
|||||||
self.actions.append(f" = {self.total}")
|
self.actions.append(f" = {self.total}")
|
||||||
|
|
||||||
def sub(self, val1, val2=None):
|
def sub(self, val1, val2=None):
|
||||||
|
"""Subtracts val2 from val1 and prints the result
|
||||||
|
then subtracts the difference from the running total.
|
||||||
|
With only one number provided, just subtract that to
|
||||||
|
the running total.
|
||||||
|
"""
|
||||||
running_total = self.total
|
running_total = self.total
|
||||||
|
|
||||||
if val2:
|
if val2:
|
||||||
@ -35,7 +50,12 @@ class Calculator():
|
|||||||
self.actions.append(f" = {self.total}")
|
self.actions.append(f" = {self.total}")
|
||||||
|
|
||||||
def mult(self, val1, val2=None):
|
def mult(self, val1, val2=None):
|
||||||
running_total = self.total
|
"""Multiplies the new values and prints the result
|
||||||
|
then multiplies the product to the running total.
|
||||||
|
With only one number provided, just mulitply the
|
||||||
|
running total with the lone value.
|
||||||
|
"""
|
||||||
|
running_total = self.total
|
||||||
|
|
||||||
if val2:
|
if val2:
|
||||||
total = val1 * val2
|
total = val1 * val2
|
||||||
@ -49,6 +69,11 @@ class Calculator():
|
|||||||
self.actions.append(f" = {self.total}")
|
self.actions.append(f" = {self.total}")
|
||||||
|
|
||||||
def div(self, val1, val2=None):
|
def div(self, val1, val2=None):
|
||||||
|
"""Divides val1 by val2 and prints the result
|
||||||
|
then divides the running total by the quotient.
|
||||||
|
With only one number provided, the running total
|
||||||
|
will be divided by the value.
|
||||||
|
"""
|
||||||
running_total = self.total
|
running_total = self.total
|
||||||
|
|
||||||
if val2:
|
if val2:
|
||||||
@ -63,6 +88,11 @@ class Calculator():
|
|||||||
self.actions.append(f" = {self.total}")
|
self.actions.append(f" = {self.total}")
|
||||||
|
|
||||||
def pow(self, val1, val2=None):
|
def pow(self, val1, val2=None):
|
||||||
|
"""Raises val1 to the power of val2 and prints
|
||||||
|
the result then raises the running total by the
|
||||||
|
result. With only one number provided, just
|
||||||
|
raise the running total by the lone value.
|
||||||
|
"""
|
||||||
running_total = self.total
|
running_total = self.total
|
||||||
|
|
||||||
if val2:
|
if val2:
|
||||||
@ -77,6 +107,9 @@ class Calculator():
|
|||||||
self.actions.append(f" = {self.total}")
|
self.actions.append(f" = {self.total}")
|
||||||
|
|
||||||
def log(self, val):
|
def log(self, val):
|
||||||
|
"""Prints the logarithm of the value. This
|
||||||
|
does not affect the running total.
|
||||||
|
"""
|
||||||
print(log(val))
|
print(log(val))
|
||||||
self.actions.append(f"log({val})")
|
self.actions.append(f"log({val})")
|
||||||
|
|
||||||
@ -84,13 +117,19 @@ class Calculator():
|
|||||||
# self.actions.append(f" = {self.total}")
|
# self.actions.append(f" = {self.total}")
|
||||||
|
|
||||||
def showcalc(self):
|
def showcalc(self):
|
||||||
|
"""Prints out the running history since the
|
||||||
|
last all clear command.
|
||||||
|
"""
|
||||||
for action in self.actions:
|
for action in self.actions:
|
||||||
print(action)
|
print(action)
|
||||||
|
|
||||||
def total(self):
|
def total(self):
|
||||||
|
"""Prints the running total."""
|
||||||
return(self.total)
|
return(self.total)
|
||||||
|
|
||||||
def ac(self):
|
def ac(self):
|
||||||
|
"""Resets the running total to 0.0 and
|
||||||
|
empties the history."""
|
||||||
self.total = 0.0
|
self.total = 0.0
|
||||||
self.actions = []
|
self.actions = []
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user