from math import log class Calculator(): """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): self.total = 0.0 self.actions = [] 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 if val2: total = val1 + val2 print(total) self.actions.append(f"{val1} + {val2}") else: total = val1 self.actions.append(f"{val1} + {running_total}") self.total += total self.actions.append(f" = {self.total}") 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 if val2: total = val1 - val2 print(total) self.actions.append(f"{val1} - {val2}") else: total = val1 self.actions.append(f"{val1} - {running_total}") self.total -= total self.actions.append(f" = {self.total}") def mult(self, val1, val2=None): """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: total = val1 * val2 print(total) self.actions.append(f"{val1} * {val2}") else: total = val1 self.actions.append(f"{val1} * {running_total}") self.total *= total self.actions.append(f" = {self.total}") 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 if val2: total = val1 / val2 print(total) self.actions.append(f"{val1} / {val2}") else: total = val1 self.actions.append(f"{val1} / {running_total}") self.total /= total self.actions.append(f" = {self.total}") 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 if val2: total = val1 ** val2 print(total) self.actions.append(f"{val1} ** {val2}") else: total = val1 self.actions.append(f"{val1} ** {running_total}") self.total = total ** self.total self.actions.append(f" = {self.total}") def log(self, val): """Prints the logarithm of the value. This does not affect the running total. """ print(log(val)) self.actions.append(f"log({val})") # self.total = log(val) ** self.total # self.actions.append(f" = {self.total}") def showcalc(self): """Prints out the running history since the last all clear command. """ for action in self.actions: print(action) def total(self): """Prints the running total.""" return(self.total) def ac(self): """Resets the running total to 0.0 and empties the history.""" self.total = 0.0 self.actions = []