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 Pop

Youssef Moustahib
Youssef Moustahib
7,779 Points

Why does it print wrong?

I want to print the items position, and the last item added for instance:

  1. kfc
  2. coffee
  3. ice cream

But what I get is:

1.kfc

1.coffee 2.coffee

  1. ice cream 2.ice cream
  2. ice cream

Here is my code:

https://w.trhou.se/kgdlbepjjq

Please Help!

2 Answers

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

Hi there! I can only assume you meant the vending2.py in your workspace. I forked it and took a look and reworked your indo function to print out what I think you're asking for.

def indo():
    index = 1
    for word in snacks:
        print("{}. {}.".format(index, word))
        index += 1

The way you had it before you were saying essentially this: "For every item in the "snacks" list print out the last item in the snacks list". So for example, if you had three items in the list, but coffee appeared last in the list, it would have printed out "3. coffee" and while the 3 would have been updated because you're incrementing, the last item in the list has not changed during this.

Hope this helps! :sparkles:

Youssef Moustahib
Youssef Moustahib
7,779 Points

Hi Jennifer I've modified this code, I want it to exit when the item "water" is trying to be added but it won't work, could you take a look? Vending2.py again.

https://w.trhou.se/o9ldadpwtc

Thank you

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

youssef moustahib Hi there! I really don't know what to tell you here. When I fork your workspace and run the code, any time I type in any variant of water including "water" or "WATER", it exits the program. Are you positive you spelled water correctly and are you positive you saved your file?

Youssef Moustahib
Youssef Moustahib
7,779 Points

Jennifer Nordell Hi Jen,

I think I may have explained it wrong. I want to append all of the 'drinks' except for water. so when it prompts me for what I would like to have, I keep answering 'drinks'. Then once I answer with 'drinks' but water is the next thing in the list I want it to exit.

So I dont want to type 'water' I just want to keep appending 'drinks', once it hits water it should exit.