There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
I got a message for the very last part that asked: Does the size return True when it is equal to 0? My answer was yes, but it didn’t think so. I got fed up and looked at the solution.
MY CODE
# Define has_space() and is_empty() below:
def has_space(self):
if self.limit > self.size:
return True
else:
return False
def is_empty(self):
if self.size == 0:
return True
else:
return False
I understand my method isn’t the best way and the solution is definitely better.
If i understand what you are saying. Technically my code should work (that it is equivalent), but it’s so redundant that the exercise didn’t accept it?
Redundant might not be the appropriate word. Naive is the word I chose. The SCT author wants us to move away from the beginner style to the intermediate. Technically there is nothing wrong with your code as it stands, just that it uses literals instead of expressions.
As I understand, Stacks not always have a limit. In this case, should function has_space take this option into consideration, so it does not raise an error when limit = None?
This is a good explanation and I understand about moving us learners to the next stage. It is a bit frustrating though when the instructions actually say, “The method should return True if the stack’s size is 0.”
One thing I’ve found about coding is that every day is a school day.
It is frustrating because it allows the “if… return True” statement in the has_space method, but disallows it in the is_empty method, and offers no explanation as to why. With us basically left wondering why this code won’t work. I understand if the author is trying to move us past such naive statements, but this isn’t really made clear, or even explained at all in the lesson.
Just looking at dates, it’s interesting that I’m having the same issue that was discussed two years ago.
in fact even redoing the lesson copy/pasting the solution doesn’t help in my case.
I have noticed I am having to do this more lately, that is to clear the code and start the exercise from scratch. It usually works after I do this, and I know that I change nothing because I just copy the code into notepad and paste it back in.
Can you make it so that self refers to the class like when you use def? You can just type the classes name if you want to call the method. What if you can call this method like this. If you didn’t send me that post then I would use the has_space() method like this: stack = Stack(10) stack.has_space()
How do you make these lines of code go into multiple lines? How many backticks?
Will this throw an error? I think it would because you have to pass an argument that corresponded to self unlike in an equivalent def where the method thinks it refers to the class. I now think that because you said self was a required argument because the lambda doesn’t refer the first parameter as the class.
Can you explain this more? This code is meant to be in a class.
What we call our instance within the class is arbitrary, with the convention being to give the name, self. We could call it this or that or anything we wish. Once we define the name with __init__() (give it attributes), then self becomes the instance object itself (by local reference).
def is_empty(self):
return self.size < 1
Given an instance, a,
a.is_empty()
Will report back on that instance. Is that what your lambdas do, as well? If that is the case, then ignore my earlier comment. Perhaps it’s just that one is not used to seeing lambdas as instance methods.
How commonly are lambdas used to make methods? Why is/isn’t it a good practice to do that?
print("1 2") # Four spaces between 1 and 2.
print("1 2") # A manual tab between 1 and 2.
print("1 2") # Six spaces.
print("1\t2") # A coded tab.
Why does 4 spaces take the same space as a manual tab? Why does 6 spaces take the same space as a coded tab? Why are the amount of spaces different? Why does a manual tab take a different amount of space as a coded tab?