Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,244 members, 7,807,825 topics. Date: Wednesday, 24 April 2024 at 08:00 PM

Please Help With My Python Shopping Cart Script - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Please Help With My Python Shopping Cart Script (2256 Views)

My Python Journey / How Can I Set Up Or Run My Python IDE On VS Code On Windows 8 / Please Review My Price comparison engine (2) (3) (4)

(1) (Reply) (Go Down)

Please Help With My Python Shopping Cart Script by shady1234: 3:07pm On Mar 15, 2017
I applied for the Andela fellowship and have completed all quizzes and labs except this shopping cart lab. My solution has passed all visible tests but I don't know why it's failing to pass the hidden tests. Can anybody tell me what's wrong with my code or what hidden test they think I'm failing? Below is my code


class ShoppingCart(object):
def __init__(self):
self.total = 0
self.items = dict()
def add_item(self, item_name, quantity, price):
if item_name and quantity and price:
if type(item_name) == str and type(quantity) == int and type(price) == int:
if item_name in self.items:
self.items[item_name] += quantity
else:
self.items[item_name] = quantity
self.total += quantity * price
def remove_item(self, item_name, quantity, price):
if item_name and quantity and price:
if type(item_name) == str and type(quantity) == int and type(price) == int:
if item_name in self.items:
if quantity > self.items[item_name]:
del self.items[item_name]
else:
self.items[item_name] -= quantity
self.total -= quantity * price
def checkout(self, cash_paid):
if cash_paid and type(cash_paid) == int:
if self.total > cash_paid:
return "Cash paid not enough"
else:
return cash_paid - self.total
class Shop(ShoppingCart):
def __init__(self):
self.quantity = 100
def remove_item(self):
self.quantity -= 1
Re: Please Help With My Python Shopping Cart Script by shady1234: 5:40pm On Mar 15, 2017
.
Re: Please Help With My Python Shopping Cart Script by shady1234: 5:40pm On Mar 15, 2017
shady1234:
So I have been able to solve this. For those who are still having problems with this test. The problem was in the remove_item method. If you use the supplied quantity to compute the total, you'll get a wrong answer if the quantity supplied is greater than what is already in the cart. The correct remove_item method is:


def remove_item(self, item_name, quantity, price):
if item_name and quantity and price:
if type(item_name) == str and type(quantity) == int and type(price) == int:
if item_name in self.items:
if quantity >= self.items[item_name]:
self.total -= (self.items[item_name] * price)
del self.items[item_name]
else:
self.items[item_name] -= quantity
self.total -= quantity * price

Re: Please Help With My Python Shopping Cart Script by noordean(m): 6:28pm On Mar 15, 2017
shady1234:
Your code has been cutt-off. Maybe you should attach a screenshot

(1) (Reply)

PHP Socket Programming To Send And Receive XML File Data / Tutorial On Git And Github / How Much Would It Cost Me To Create A Jamb Past Question App

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 11
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.