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 Basics (2015) Shopping List App Break

John Schut
John Schut
2,317 Points

breaks.py assignment

My solution returns the wrong items..?

def loopy(items): # Code goes here items_total = ["Snoep", "Melk", "Kaas"] items_total.append(items) for items in items_total: if items == "STOP": break
print(items)

loopy("STOP")

breaks.py
def loopy(items):
    # Code goes here
    items_total = ["Snoep", "Melk", "Kaas"]
    items_total.append(items) 
    for items in items_total:
        if items == "STOP":
            break    
        print(items)

loopy("STOP")

5 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

It returns values they aren't expecting... yes. Because you're appending your own information to theirs. Remember during challenges: try not to add variables and information they aren't looking for. Your output can be something they don't expect. Take a look at my solution.

def loopy(items):
    # Code goes here
    for item in items:
      if(item == 'STOP'):
        break
      else:
        print(item)
John Schut
John Schut
2,317 Points

Hi Jennifer,

Again, thank you very much!

John Schut
John Schut
2,317 Points

Hi Jennifer,

Can you please help me with the following (from someone else I get hints, but not the solution and I want to go on -> I think I am 99% there... :-) ).

See please my solution below.

It returns 0 in stead of 2.

It has maybe to do with the fact that a list-item is not the same datatype as the key of a dictionary...(?)

counts.py

You can check for dictionary membership using the

"key in dict" syntax from lists.

Example

my_dict = {'apples': 1, 'bananas': 2, 'coconuts': 3}

my_list = ['apples', 'coconuts', 'grapes', 'strawberries']

members(my_dict, my_list) => 2

my_dict = {'apples': 1, 'bananas': 2, 'coconuts': 3} my_list = ['apples', 'coconuts', 'grapes', 'strawberries']

def members(dictionary, mylist): count = 0 for item in mylist: try: if dictionary[item] in [1,2,3]: count += 1 except: break

print(count) # Isn't necessary, but helps me with finding the correct solution. return(count)

members(my_dict, my_list) # Isn't necessary, but helps me with finding the correct solution.

If I delete the print and the memberscall, the solution is still not correct...

Strange thing is, that when I do this in a seperate workspace it does return 2. Strange...

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Can you link the challenge you're working on? You've gone past the challenge listed at the top here :)

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I found the challenge you're working on! I'll show you what I did then walk you through what we're doing here.

def members(myList, myDict):
  count = 0;
  for item in myList:
    if item in myDict:
      count += 1
  return count

Ok so the nice fellows/gals over at Treehouse are going to send your members function some info and you need to return the count of the items where the item in the list is also a key in the dictionary. So we start by making a count variabe (you can name this anything) and setting it to 0. Then we look at every item in myList. Now, if that item is in myDict then we increment the count variable. At the very end when it's all said and done, we return the final count. Hope this clears things up!

John Schut
John Schut
2,317 Points

Hi Jennifer, thanks again!

I made it harder then it should. But the strange thing of your solution is that item in myDict is apprendly good enough, although myDict excists of two parts (and I thought you had to be specific in naming the correct one)...