Ok, I actually didn’t notice this until I saw a similar format in the quiz to follow this… Notice the if: else: statements below:
def push(self, value):
if self.has_space():
item = Node(value)
item.set_next_node(self.top_item)
self.top_item = item
self.size += 1
print("Adding {} to the pizza stack!".format(value))
else:
print("No room for {}!".format(value))
I don’t recall learning this following. It appears to be an if else statement without the else? Is that right? When did we learn this, so that I can go back to it an review it? I tried to remove the else in above statement to see if it worked that way, and it didn’t. It printed No room for {}!".format(value) each time. So what’s happening?
def pop(self):
if not self.is_empty():
item_to_remove = self.top_item
self.top_item = item_to_remove.get_next_node()
self.size -= 1
print("Delivering " + item_to_remove.get_value())
return item_to_remove.get_value()
print("All out of pizza.")
def peek(self):
if not self.is_empty():
return self.top_item.get_value()
print("Nothing to see here!")
Why is the pizza stack used as an example?
Wouldn’t this be a bad use case for a LIFO data structure?
Wouldn’t making / delivering pizza be more suited for a queue / FIFO structure?
Came here wondering the same thing! My guess is they were initially going for a ‘stacking empty pizza boxes’ example and then changed it to deliveries halfway through to make it a more fun example.
This is good to know about using return and else, however, why is the return statement being used in the first place when the call to the pop() method, pizza_stack.pop(), isn’t using the returned value?
They do the same thing and will only print if the condition is false.
The difference is more a matter of style and how explicit you like to be. Using an else can certainly help follow the logic of the program. The version without an else is a bit trickier and takes some programming knowledge to understand what is happening an how to use it. At this point in the course, it looks like codecademy trusts that students have that knowledge to understand it or know how to find out like asking on here.
That’s not true. The if/else will only do one or the other. The second snippet will only do the if statement if the condition is met AND always do the print.
To clarify why they are different, since I said that, in this case they are not. But only in this one case. Had the return been anything else, then they are not and never are the same. That’s why they are still different. The difference is the return, not the logic that went into it.
This
if condition:
print(something)
else:
print(something)
Or whatever you want to put in that line will never do this
if condition:
return value
print(something)
Unless it was a return in this one specific case. To me its misleading to say that is how it is an why I disagreed.
You’re right that this structure only works if the conditional leads to a return statement. That specific case is what the whole thread was about though. I didn’t cover other uses of an if-else clause because anyone who has made it this far in the course should have a decent understanding of them. The return statement makes it possible to do an if-else without the else, but that method is a bit trickier and non obvious for those who haven’t seen it before.