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 Introducing Lists Build an Application The Application

Is there a way to short cut this code/script?

Hi guys, is there a way to shortcut this code/script?

while True: new_item = input("> ")

if new_item == 'DONE'.lower():
    break
elif new_item == 'DONE'.upper():
    break
elif new_item == 'Done':
    break

Thanks

4 Answers

if new_item.lower() == "done": break

You may also try,

while True:
new_item = input("> ")  
new_item2 = new_item.upper()
if new_item2 == 'DONE':
    break
Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Leonard,

Instead of comparing the exact text that the user entered with all the combinations of upper and lower case you can think of, consider just comparing an all-lower (or upper) case version of the user's input to just the corresponding version of 'done'.

Cheers

Alex

Edgar huamantla
Edgar huamantla
1,421 Points

For the purpose of evaluating the condition, you could force it to new_item.lower(). But it will return back to its original value.

example:

new_item = 'DonE'

if new_item.lower() == 'done':
   break

(This will evaluate to true)

print(new_item) would show 'DonE' because the string was passed to the lower method. The lower method returned a new string 'done' for the purpose of evaluating the condition.