please who hear is a good python programmer

Hagoromo sennin

Leaf Village Regular 🍃
Regular
Joined
Sep 3, 2016
Messages
902
Reaction score
72
I need your help guys,pls!
i have a deadline to meet and i need to submit this code,but it passes the visible test,but not the hidden one.
heres the test
import unittest

class ShoppingCartTestCases(unittest.TestCase):
def setUp(self):
self.cart = ShoppingCart()
self.shop = Shop()

def test_cart_property_initialization(self):
self.assertEqual(self.cart.total, 0, msg='Initial value of total not correct')
self.assertIsInstance(self.cart.items, dict, msg='Items is not a dictionary')

def test_add_item(self):
self.cart.add_item('Mango', 3, 10)

self.assertEqual(self.cart.total, 30, msg='Cart total not correct after adding items')
self.assertEqual(self.cart.items['Mango'], 3, msg='Quantity of items not correct after adding item')

def test_remove_item(self):
self.cart.add_item('Mango', 3, 10)
self.cart.remove_item('Mango', 2, 10)

self.assertEqual(self.cart.total, 10, msg='Cart total not correct after removing item')
self.assertEqual(self.cart.items['Mango'], 1, msg='Quantity of items not correct after removing item')

def test_checkout_returns_correct_balance(self):
self.cart.add_item('Mango', 3, 10)
self.cart.add_item('Orange', 16, 10)

self.assertEqual(self.cart.checkout(265), 75, msg='Balance of checkout not correct')
self.assertEqual(self.cart.checkout(25), 'Cash paid not enough', msg='Balance of checkout not correct')

def test_shop_is_instance_of_shopping_cart(self):
self.assertTrue(isinstance(self.shop, ShoppingCart), msg='Shop is not a subclass of ShoppingCart')

def test_shop_remove_item_method(self):
for i in range(15):
self.shop.remove_item()

self.assertEqual(self.shop.quantity, 85)
and heres my own

class ShoppingCart(object):

def __init__(self,total=0,items = {} ):
self.total = total
self.items = items
def add_item(self,item_name="",quantity =0,price = 0):
self.total += price*quantity
self.items = {item_name:quantity}

def remove_item(self, item_name="", quantity =0, price=0):
self.total -= price*quantity
if self.items[item_name] <= quantity:
del self.items
else:
self.items[item_name] -= quantity
def checkout(self,cash_paid):
balance = 0
if cash_paid < self.total:
return "Cash Paid Not Enough"
else:
balance = cash_paid -self.total
return balance

class Shop(ShoppingCart):

def __init__(self,quantity=100):
self.quantity = quantity

def remove_item(self):
self.quantity -= 1
 

Sauce

Jōnin Strategist 🧠
Veteran
Joined
Apr 7, 2014
Messages
2,240
Reaction score
284
My answer just 404'd since idk wtf this shit means
 

Nilla

Member
Joined
Jun 24, 2015
Messages
17
Reaction score
3

^
don't know if codecademy is still active or not... but check the python section it might be useful
 

nk07ki30

Jōnin Strategist 🧠
Regular
Joined
Nov 8, 2016
Messages
1,018
Reaction score
79
I wish i knew how to do this shit.

Then learn. If you have the apitude for it then go on YouTube and learn how to code, the YouTuber "NerdGasm" can help you out, he has tutorials on coding. Good luck.
 

Iruka

Leaf Village Regular 🍃
Regular
Joined
May 1, 2011
Messages
697
Reaction score
72
class ShoppingCart(object):

def __init__(self, total = 0, items = {}):
self.total = total
self.items = items

def add_item(self, item_name= "", quantity = 0, price = 0):
self.total += price * quantity
self.items = {item_name:quantity} # Comment 1

def remove_item(self, item_name= "", quantity = 0, price = 0):
self.total -= price * quantity # Comment 2

if self.items[item_name] <= quantity:
del self.items # Comment 3
else:
self.items[item_name] -= quantity

def checkout(self,cash_paid):
balance = 0 # Comment 4 (Part A)

if cash_paid < self.total:
return "Cash Paid Not Enough"
else:
balance = cash_paid -self.total # Comment 4 (Part B)
return balance # Comment 4 (Part C)




class Shop(ShoppingCart):

def __init__(self, quantity = 100):
self.quantity = quantity

def remove_item(self):
self.quantity -= 1 # Comment 5

I imagine I am too late to the party to be of help sorry. I'm also extremely out of practice regarding Python, that said, you've got some quirks here.

Comment 1
Your add_items method is not adding an item. It is creating a new dictionary and assigning it to your persistent items variable. This could be failing a hidden test where two different items are added to the cart as the second call to add_items will completely overwrite your first item.

>> myCart = ShoppingCart()
>> myCart.add_item("Apple", 5, 10)

Your cart should now hold 5 Apples, and its total should be 50:
>> {'Apple': 5}
>> 50

>> myCart.add_item("Banana", 3, 5)

Your cart should now hold 5 Apples and 3 Bananas, and its total should be 65:
>> {'Apple: 5, 'Banana': 3}
>> 65

Instead, your cart.items has been completely overwritten while its cart.total has not. You will find your cart being:
>> {'Banana': 3}
>> 65

Your cart is now broken.


Comment 2
You are decreasing the price before checking whether you have exceeded the number of items able to be removed. This is incredibly dangerous. If I add 5 apples to my cart then ask to remove 8 apples, you are removing the cost of 8 apples from cart.total which shouldn't be possible. This would only work if you had safety measures elsewhere in your system. Something to consider in the future.


Comment 3
Your call del self.items is not deleting the (key: value) for a single item, it is calling a delete on the entire items dictionary. As with Comment 1, if you have multiple items added, you've effectively just wiped your entire cart.


Comment 4
This is not very Pythonic. Why create a variable for balance, initialize it to zero, then assign it a value, then return it?

>> return cash_paid - self.total

This achieves the same purpose.


Comment 5
It may not be a requirement of your assignment but you have no checks to make sure this variable cannot go negative. Something to consider in the future.
 
Last edited:

Hagoromo sennin

Leaf Village Regular 🍃
Regular
Joined
Sep 3, 2016
Messages
902
Reaction score
72
I imagine I am too late to the party to be of help sorry. I'm also extremely out of practice regarding Python, that said, you've got some quirks here.

Comment 1
Your add_items method is not adding an item. It is creating a new dictionary and assigning it to your persistent items variable. This could be failing a hidden test where two different items are added to the cart as the second call to add_items will completely overwrite your first item.

>> myCart = ShoppingCart()
>> myCart.add_item("Apple", 5, 10)

Your cart should now hold 5 Apples, and its total should be 50:
>> {'Apple': 5}
>> 50

>> myCart.add_item("Banana", 3, 5)

Your cart should now hold 5 Apples and 3 Bananas, and its total should be 65:
>> {'Apple: 5, 'Banana': 3}
>> 65

Instead, your cart.items has been completely overwritten while its cart.total has not. You will find your cart being:
>> {'Banana': 3}
>> 65

Your cart is now broken.


Comment 2
You are decreasing the price before checking whether you have exceeded the number of items able to be removed. This is incredibly dangerous. If I add 5 apples to my cart then ask to remove 8 apples, you are removing the cost of 8 apples from cart.total which shouldn't be possible. This would only work if you had safety measures elsewhere in your system. Something to consider in the future.


Comment 3
Your call del self.items is not deleting the (key: value) for a single item, it is calling a delete on the entire items dictionary. As with Comment 1, if you have multiple items added, you've effectively just wiped your entire cart.


Comment 4
This is not very Pythonic. Why create a variable for balance, initialize it to zero, then assign it a value, then return it?

>> return cash_paid - self.total

This achieves the same purpose.


Comment 5
It may not be a requirement of your assignment but you have no checks to make sure this variable cannot go negative. Something to consider in the future.

thanks bo,already solved it though
 
Top