![]() |
Learning about if statements - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Learning about if statements (/thread-30964.html) |
Learning about if statements - kam_uk - Nov-15-2020 Bello, I am learning about if statements. The question is to assign True or False to variables small and green. Then write some if/else statements to print which of these matches those choices; cherry, pea, watermelon and pumpkin. Which of these two is better? Which is more pythonic? small = True green = False if small and green: print("pea") elif small and not green: print("cherry") elif not small and green: print("watermelon") elif not small and not green: print("pumpkin")or small = True green = False if small: if green: print("pea") else: print("cherry") else: if green: print("watermelon") else: print("pumpkin") RE: Learning about if statements - jefsummers - Nov-15-2020 I would argue for the top version, from the point of view of maintainability and expandability. What happens if another fruit is introduced, apple? Add a couple lines to the first one and you are fine. But, how do you fix the second code snippet to handle apples? And then they add coconuts. And they migrate. |