Python Introducing Lists Build an Application Add Items

Michael Casella
Michael Casella
1,358 Points

Why was add_to_list in the while loop not an else statement?

Why was add_to_list in the while loop not an else statement?

2 Answers

Steven Parker
Steven Parker
195,257 Points

An "else" is not needed there since both the "if" and "elif" conditions change the program flow, so all the code below them is done only in an "else" condition. This is a common technique to make the code more compact and easier to maintain.

Shanil Mohan R
Shanil Mohan R
1,492 Points

Like steven mentioned else is not needed here to keep the code more readable and compact. But you are right. There is no harm in putting the add_to_list function in an else block.