Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Lists Shopping List Take Three

Mikael D.D
PLUS
Mikael D.D
Courses Plus Student 2,080 Points

question about DRYING myself

I was wondering if it would be possible to write this line of code without repeating the variable name 4 times. What if, for instance, I had more options than quit and done ?

Im just trying to devellop my DRY spydie sense if that make sense.

Thanks in advance

if new_item.upper() == "DONE" or new_item.upper() == "QUIT" or new_item.upper() == "STOP"  or new_item.upper() == "FINISH":

1 Answer

Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

Hi Mikael D.D. If you are using that if statement, then you can create a list

option_list = ['DONE', 'QUIT', 'STOP', 'FINISH']

You can use just the list

 new_item.upper() in option_list:

Even if you have multiple options not just quit or done or whatever, you can still use a list as options the only difference would be a reference for the right option in the list

option_list = ['DONE', 'QUIT', 'STOP', 'FINISH', 'START', 'PAUSE']

if new_item.upper() == option_list[4]:   --> 4 is the position of the START option in the list
    <start an action>
elif new_item.upper() == option_list[5]:   --> 5 is the position of the PAUSE option in the list
    <pause the program or any action>

I hope this illustration will help you out. Happy coding

Mikael D.D
Mikael D.D
Courses Plus Student 2,080 Points

Very interesting approach and you did it while staying on the topic of Lists..Very cool....It really shows the versatility of lists and how we can use them in so many ways.. Thank you Oszkár very appreciated.