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 trialSam Morpeth
8,756 PointsI found this video incredibly confusing and I can't seem to replicate the outcomes of it.
This is the first video on Treehouse where I felt I was just blindly trying to type exactly what the teacher was telling me to. Importing something so I could clear the screen felt like a needlessly confusing way to start the video. Also, a few of the concepts like None
aren't brought up in previous videos and it really threw me for a loop.
Below is my code which is causing me all sorts of problems. For one thing whenever I type an item into the app it comes out with a blank list and says "2 items added to your list" when I can't actually see anything. It then prints a list like this "1. Apples
- Apples"
Essentially doubling the items that are printed when I can't see why that would happen in the code. It also does the same for the next item. I also can't seem to get the items assigned to specific positions.
python
import os
shopping_list = []
def clear_screen():
os.system("cls" if os.name == "nt" else "clear")
def show_help():
print("What should we pick up at the store?")
print("""
Enter 'DONE' to stop adding items.
Enter 'HELP' for this help.
Enter 'SHOW' to see your current list.
""")
def add_to_list(item):
show_list()
if len(shopping_list):
position = input("Where should I add {}?\n"
"Press ENTER to add to the end of the list\n"
">".format(item))```
else:
position = 0
try:
position = abs(int(position))
except ValueError:
position = None
if position is not None:
shopping_list.insert(position -1, item)
else:
shopping_list.appen(new_item)
shopping_list.append(new_item)
print("Added! List has {} items.".format(len(shopping_list)))
def show_list():
clear_screen()
print("Here's your list:")
index = 1
for item in shopping_list:
print("{}. {}".format(index, item))
index += 1
print("-" * 10)
show_help()
while True:
new_item = input("> ")
if new_item.upper() == 'DONE' or new_item.upper() == 'QUIT':
break
elif new_item.upper() == 'HELP':
show_help()
continue
elif new_item.upper() == 'SHOW':
show_list()
continue
else:
add_to_list(new_item)
show_list()
I'm sure I've done something fundamentally wrong, but I found this video incredibly confusing and very frustrating.
4 Answers
andren
28,558 PointsThe issue stems from the fact that you seem to have missed some changes that Kenneth made around the 7:20 mark in the video. At that point he removes the last two lines of the add_to_list
function:
shopping_list.append(new_item)
print("Added! List has {} items.".format(len(shopping_list)))
The important one is that first line, that line was made redundant when the code that allowed you to specify the position was added. Because as part of that change you added the following if
statement:
if position is not None:
shopping_list.insert(position -1, item)
Which inserts the item into the list. If you keep both the shopping_list.insert
and shopping_list.append
lines then you end up with the item being inserted twice.
Kenneth also adds an additional show_list()
call at the end of the function, so that the list is printed out after adding an item. If you don't add that line then you won't see what item you just added unless you use the "show" command or add another item.
Looking at your script I also notice that you have a typo in your else
statement:
else:
shopping_list.appen(new_item)
You have typed appen
instead of append
. That typo will result in a crash if you try to add an item without specifying the position. Besides that your code seems fine.
The video does indeed introduce quite a few new concepts, and is one of the more complex scripts shown off in the beginner courses. If there are any specific parts of the script that confuses you then feel free to ask about them, and I'll do my best to clarify what is going on and why it is written that way.
Alonso Martinez
8,442 PointsCan Craig re-do these videos? lol
Sam Morpeth
8,756 PointsOh ok! That's exactly what I needed. Thank you very much. Thanks for explaining why it was repeating the item. I'm not sure I would have noticed that otherwise.
I think I was just confused by the new concepts and left with a lot of questions after the video. I'm not even sure what those questions would be, nor do I think I could understand the answers at the moment! But thanks anyway.
andren
28,558 PointsNo problem . And yeah it is completely normal to be a bit confused when new concepts are first introduced, but as you continue with the courses and get to see these concepts used in different ways you'll likely start to wrap your mind around them more and more.
To be honest it was much the same for me when I started out programming. It sometimes took me quite some time to really wrap my head around new concepts that i had not touched on before, even concepts that I now feel are very simple and intuitive. So I hope you don't feel too discouraged. As long as you keep practicing and studying the language you will soon look back at this lesson and have no problem understanding it at all.
ms28
Python Web Development Techdegree Student 1,067 PointsI found it confusing as well!