Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8981677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:23:45+00:00 2026-06-15T20:23:45+00:00

I am studying for my final and this was a quiz question I missed.

  • 0

I am studying for my final and this was a quiz question I missed. I need most of the help on the getTotal method. I need to loop through the list, find the price of each item, add the price to the total and return the total. I struggle with loops and I am not sure how to pull the second item out of a list.. [1] ?? I have tried many ways and am getting frustrated.

If there is anyone up that is willing to help me that would be great. I am still learning and am new at this so go easy on me, but I really want to learn it. It’s probably not as hard as I make it out to be, but Ill be waiting for some input. Thank you!

class Item: 
    def __init__(self, name, price):
        self.name = name
        self.price = price

    def getPrice(self):
        return self.price

    def getName(self):
        return self.name

class Cart:
    def __init__(self, list):
        self.list = []

    def addItem(self, item):
        self.list.append(self.list)

    def getTotal(self):
        total = 0
        for item in self.list:
            name, price = item # or price = item[1]
            total = total + price

    def getNumItems(self):
        count = 0
        for c in range(self.list):
            count = self.list + 1
            return count

    def removeItem(self, item)
        #removes the item from the cart's item list

def main():
    item1 = Item("Banana", .69)
    item2 = Item("Eggs", 2.39)
    item3 = Item("Donut", .99)
    c = Cart()
    c.addItem(item1)
    c.addItem(item2)
    c.addItem(item3)
    print "You have %i items in your cart for a total of $%.02f" %(c.getNumItems(), c.getTotal())
    c.removeItem(item3)
    print "You have %i items in your cart for a total of $%.02f" % (c.getNumItems(), c.getTotal())
main()  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:23:47+00:00Added an answer on June 15, 2026 at 8:23 pm

    Here gives the time and I changed the code and now it is fully functional shopping cart

    class Item(object): 
        def __init__(self, unq_id, name, price, qty):
            self.unq_id = unq_id
            self.product_name = name
            self.price = price
            self.qty = qty
    
    
    class Cart(object):
        def __init__(self):
            self.content = dict()
    
        def update(self, item):
            if item.unq_id not in self.content:
                self.content.update({item.unq_id: item})
                return
            for k, v in self.content.get(item.unq_id).iteritems():
                if k == 'unq_id':
                    continue
                elif k == 'qty':
                    total_qty = v.qty + item.qty
                    if total_qty:
                        v.qty = total_qty
                        continue
                    self.remove_item(k)
                else:
                    v[k] = item[k]
    
        def get_total(self):
            return sum([v.price * v.qty for _, v in self.content.iteritems()])
    
        def get_num_items(self):
            return sum([v.qty for _, v in self.content.iteritems()])
    
        def remove_item(self, key):
            self.content.pop(key)
    
    
    if __name__ == '__main__':
        item1 = Item(1, "Banana", 1., 1)
        item2 = Item(2, "Eggs", 1., 2)
        item3 = Item(3, "Donut", 1., 5)
        cart = Cart()
        cart.update(item1)
        cart.update(item2)
        cart.update(item3)
        print "You have %i items in your cart for a total of $%.02f" % (cart.get_num_items(), cart.get_total())
        cart.remove_item(1)
        print "You have %i items in your cart for a total of $%.02f" % (cart.get_num_items(), cart.get_total())
    

    And a output is:

    You have 8 items in your cart for a total of $8.00
    You have 7 items in your cart for a total of $7.00
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm studying for a final and I can't figure this question out: Suppose that
I'm studying for a final exam and I stumbled upon a curious question that
I am studying for a final tomorrow in C, and have a question regarding
Im studying for finals and i came across this question: write a PHP script
I'm studying for finals and i came across this question: Write a php script
I was studying for my finals and I came across this question: write the
I'm studying for my final exams in my CS major on the subject distributed
I'm currently studying for a CS course's final exam and I've run into a
While studying for a Functional Programming exam, I came across the following question from
In studying actionscript 3's graphics class, I've come across the undocumented drawRoundRectComplex() method. It's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.