Answered step by step
Verified Expert Solution
Question
1 Approved Answer
What is wrong with my Python code: Error is: Total price: Traceback (most recent call last): File /Users//PycharmProjects/main.py, line 54, in print(locale.currency(cart.gettotalPrice())) File /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/locale.py, line
What is wrong with my Python code:
Error is: Total price: Traceback (most recent call last): File "/Users//PycharmProjects/main.py", line 54, in print(locale.currency(cart.gettotalPrice())) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/locale.py", line 270, in currency s = _format('%%.%if' % digits, abs(val), grouping, monetary=True) TypeError: bad operand type for abs(): 'NoneType'
Code:
# main.py import locale # CashRegister class class CashRegister: totalPrice: float numItems: float # initiate constructor def __init__( self ): self.numItems = 0 self.totalPrice = 0 # Method addItem takes cost as input # and add the cost to the total and # increment the numItems by 1 def addItem( self, cost ): # increment numItems by 1 self.numItems = self.numItems + 1 # add cost to total self.totalPrice = self.totalPrice + cost # main method def getnumItems( self ): pass def gettotalPrice( self ): pass if __name__ == "__main__": cart = CashRegister() print("Welcome!!") while True: n = int(input("Enter 1 to add item to cart if n == 1: totalPrice = float(input("Enter price cart.addItem(totalPrice) elif n == 0: break else: print("Invalid input...Try again") locale.setlocale(locale.LC_ALL, 'en_US') print("Total items in cart:", cart.getnumItems print("Total price:", end=' ') print(locale.currency(cart.gettotalPrice()))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started