-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
swap_meet #74
base: master
Are you sure you want to change the base?
swap_meet #74
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great job, Chan! Your code was very easy to read, very simple! My biggest feedback would be on giving Inheritance and super() lessons another read, but other than that you nailed this! :)
class Clothing(Item): | ||
|
||
def __init__(self, category = "", condition=0): | ||
self.category = "Clothing" | ||
self.condition = condition |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this works! But since you are using inheritance by importing Item
and passing it in, we can take advantage of the parent class's __init__
method that's already been constructed.
class Clothing(Item): | |
def __init__(self, category = "", condition=0): | |
self.category = "Clothing" | |
self.condition = condition | |
class Clothing(Item): | |
def __init__(self, condition=0): # because we know Clothing class will always be category "clothing", we don't need to give the user the ability to pass in a value | |
super().__init__("clothing", condition) # here we are calling the parent class's __init__ method and we pass in the argument from the Child class down to the parent with "clothing". Now we don't need to assign self.category or self.condition again |
here's another way we could do it based on Learn's super()
lesson:
class Clothing(Item): | |
def __init__(self, category = "", condition=0): | |
self.category = "Clothing" | |
self.condition = condition | |
class Clothing(Item): | |
# if we don't change the parameters and keep them exactly like the parent class's __init__ method, then we don't technically have to create a child __init__ method because it will inherit the parent's. So only the __str__ method will be in this class |
Both options work, but I would say the top suggestion is better practice, and you will see it in industry
class Decor(Item): | ||
|
||
def __init__(self, category="", condition=0): | ||
self.category = "Decor" | ||
self.condition = condition | ||
|
||
def __str__(self): | ||
return "Something to decorate your space." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 think about what I talked about in clothing.py
and apply it here as well!
class Electronics(Item): | ||
|
||
def __init__(self, category="", condition=0): | ||
self.category = "Electronics" | ||
self.condition = condition | ||
|
||
def __str__(self): | ||
return "A gadget full of buttons and secrets." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 think about what I talked about in clothing.py
and apply it here as well!
class Item: | ||
def __init__(self, item = "Hello World!", category="", condition = 0): | ||
self.category = category | ||
self.item = item | ||
|
||
def __str__(self): | ||
return f"{self.item}" | ||
|
||
def __float__(self): | ||
return f"{self.condition}" | ||
|
||
def condition_description(self): | ||
return f"{self.condition}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like self.condition
is being used in condition_description
method, but it is not assigned in the __init__
method. Let's try something like this:
class Item: | |
def __init__(self, item = "Hello World!", category="", condition = 0): | |
self.category = category | |
self.item = item | |
def __str__(self): | |
return f"{self.item}" | |
def __float__(self): | |
return f"{self.condition}" | |
def condition_description(self): | |
return f"{self.condition}" | |
class Item: | |
def __init__(self, category="", condition = 0): | |
self.category = category | |
self.condition = condition | |
def __str__(self): | |
return "Hello world!" | |
def condition_description(self): | |
if self.condition == 0: | |
return "This item is trash!" | |
# keep adding all conditions ( 0 thru 5) to this method to give a description to the item, not just the condition number |
@@ -0,0 +1,109 @@ | |||
|
|||
class Vendor: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
return item | ||
|
||
|
||
def remove(self, item): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
else: | ||
return False | ||
|
||
def get_by_category(self, category): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
def swap_first_item(self, other_vendor): | ||
|
||
|
||
if self.inventory == [] or other_vendor.inventory == []: | ||
|
||
return False | ||
|
||
else: | ||
|
||
other_vendor.inventory.append(self.inventory[0]) | ||
self.inventory.remove(self.inventory[0]) | ||
self.inventory.append(other_vendor.inventory[0]) | ||
other_vendor.inventory.remove(other_vendor.inventory[0]) | ||
|
||
return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 this works perfectly! but let's look at lines 32 - 43 above. We are almost doing the same thing twice, so how could we fix that? We could use the swap_items
method inside of this one.
def swap_first_item(self, other_vendor): | |
if self.inventory == [] or other_vendor.inventory == []: | |
return False | |
else: | |
other_vendor.inventory.append(self.inventory[0]) | |
self.inventory.remove(self.inventory[0]) | |
self.inventory.append(other_vendor.inventory[0]) | |
other_vendor.inventory.remove(other_vendor.inventory[0]) | |
return True | |
def swap_first_item(self, other_vendor): | |
return self.swap_items(other_vendor, self.inventory[0], other_vendor.inventory[0]) |
So, we call swap_items
and pass in the arguments it needs: the other vendor, our item, other vendor's item. So we grab the first item out of the inventories by using index 0. Now we let the method return True
or False
def get_best_by_category(self, category=""): | ||
items_in_category = [] | ||
|
||
for item in self.inventory: | ||
if item.category == category: | ||
items_in_category.append(item) | ||
|
||
best_item = None | ||
best_condition = 0 | ||
for item in items_in_category: | ||
if item.condition > best_condition: | ||
best_condition = item.condition | ||
best_item = item | ||
return best_item |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 heck yeah this is great! But don't lines 62 - 66 look familiar? I bet we've got a method already built for this!
def get_best_by_category(self, category=""): | |
items_in_category = [] | |
for item in self.inventory: | |
if item.category == category: | |
items_in_category.append(item) | |
best_item = None | |
best_condition = 0 | |
for item in items_in_category: | |
if item.condition > best_condition: | |
best_condition = item.condition | |
best_item = item | |
return best_item | |
def get_best_by_category(self, category=""): | |
items_in_category = get_by_category(category) | |
best_item = None | |
best_condition = 0 | |
for item in items_in_category: | |
if item.condition > best_condition: | |
best_condition = item.condition | |
best_item = item | |
return best_item |
return best_item | ||
|
||
|
||
def swap_best_by_category(self, other, my_priority, their_priority): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 beautifully done!!!
No description provided.