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 Using Lists Mutability

Alan Kuo
Alan Kuo
7,697 Points

why does function remove my first item from my list?

Please check my code below:

books = ["Learning Python", "Automate the boring stuff", "Python for Data Analysis", "fluent Python"]

video_games = ["Zelda", "Splatoon 2", "Super Mario"]

def display_wishes(title, wishes):

print(title + ":")

print("=========>", wishes[0], "<==========")

for wish in wishes:

    print("*" + wish)

display_wishes(Books, books)

display_wishes(Video_games, video_games)

The result is:

Books: ============> Learning Python <==========

  • Automate the boring stuff
  • Python for Data Analysis
  • fluent Python

Videos Games: ============> Zelda <==========

  • Splatoon 2
  • Super Mario

My question is I did use a for loop(for wish in wishes}, but the result turns out removing the first item from my list.

Just because I printed out the first item doesn't mean I want to remove it from my list.

In my mind, the first item should be printed twice because I printed out the first item, then I printed out all the items from the list.

This is an extended question so if anybody thinks I dig too deep please just ignore me.

2 Answers

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Alan,

Please ensure you follow the instructions in the Markdown Cheatsheet and use the preview button to make sure your post looks like you expect. It's much easier to follow properly formatted code than code where some of it is in markdown and some of it is in raw text.

This is how your code should look when properly formatted:

books = ["Learning Python", "Automate the boring stuff", "Python for Data Analysis", "fluent Python"]
video_games = ["Zelda", "Splatoon 2", "Super Mario"]

def display_wishes(title, wishes):
     print(title + ":")
     print("=========>", wishes[0], "<==========")
     for wish in wishes:

         print("*" + wish)

display_wishes(Books, books)

display_wishes(Video_games, video_games)

Look how much easier to follow it is.

Next, your code does not have the output you describe. Here is the traceback from executing the code you provided:

Traceback (most recent call last):
  File "temp.py", line 11, in <module>
    display_wishes(Books, books)
NameError: name 'Books' is not defined

The reason for that error is that your function calls use Books and Video_games instead of "Books" and "Videos Games".

When we correct those errors, this is the output we get:

Books:
=========> Learning Python <==========
*Learning Python
*Automate the boring stuff
*Python for Data Analysis
*fluent Python
Videos games:
=========> Zelda <==========
*Zelda
*Splatoon 2
*Super Mario

This seems to be exactly the output you wanted to get?

This is with Python version 3.6.5, which Python are you using?

Cheers

Alex

Alan Kuo
Alan Kuo
7,697 Points

Thank you for your kindness.

I'm sure my code is correct because I ran it and the output is as described.

I just copied my code and pasted on the website except they took some of my spaces from my lines.

I don't really know how to mark my code the mark was done automatically but I'll make sure it's clear next time.

You haven't really answered my question because I wanted to know that why I asked the computer to print out my first item then I asked the computer again to print out my list.

I think there's should be 2 first items printed out but instead they just printed out the first item once.

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Alan,

I'm not aware of any version of Python where if you call a function with an undefined variable name it will be automatically transformed into a string. That, and the unexpected behaviour that you described (different from the behaviour I saw in Python v3.6.5) is why I asked which version of Python you were using.

I've pasted exactly the result of running your code in my answer and specified the version of Python that gave that result. If you run the code you provided in the version of Python that I stated, you will get the output I posted, not the output you describe. Thus the answer to your question, at least for current Python, is that it doesn't do what you describe happening to you, it does exactly what you say it should do (prints the first item in both the title and body) (as long as you fix the bugs in your function calls first).

However, I believe you when you say that what you've pasted into the question is exactly the code you ran, and exactly the response you got. So in order to provide any other answer to your question than the answer I've already given you, I'd need to know which version of Python you are seeing this in.

Cheers

Alex

Alan Kuo
Alan Kuo
7,697 Points

Thanks Alex,

I also ran the code one more time on Workspace and it turned out just what I wanted.

I might ran this on REPL with Python 3.

I think the problem is solved and I don't feel like digging in further if the problem is from the version of Python, I'm just a noob after all.

Thank you again and hope you have a great day!