Python help

I can't figure out this code. please help. I'm new to coding so any help would be nice.

import sys

itemsInBackpack = ["book", "computer", "keys", "travel mug"]

while True:
print("Would you like to:")
print("1. Add an item to the backpack?")
print("2. Check if an item is in the backpack?")
print("3. Quit")
userChoice = input()

if(userChoice == "1"):
print("What item do you want to add to the backpack?")
itemToAdd = input()

####### YOUR CODE HERE ######
#add the item to the backpack
####### YOUR CODE HERE ######

if(userChoice == "2"):
print("What item do you want to check to see if it is in the backpack?")
itemToCheck = input()

####### YOUR CODE HERE ######
#Print out if the user's item is in the backpack
####### YOUR CODE HERE ######

if(userChoice == "3"):
sys.exit()

Other urls found in this thread:

docs.python.org/3/tutorial/datastructures.html
learnpythonthehardway.org/book/
twitter.com/SFWRedditImages

itemsInBackpack.append(itemToAdd)

#add the item to the backpack
itemsInBackpack = ["book", "computer", "keys", "travel mug", itemToAdd ]

#Print out if the user's item is in the backpack
for i in range(len(itemsInBackpack)):
if itemsInBackpack[i] == itemsInBackpack:
print("Already in backup kys kys")

Format your code!

I'll help. but only because your homework is in Python

adding to the backpack
itemsInBackpack.append(itemToAdd)


Cheking if item is in backpack
if itemToCheck in itemsInBackpack:
print('item is in the backpack')
else:
print('Nope.')


printing items:
print('Items in backpack: {0}'.format(', '.join(itemsInBackpack)))

Format your code, I tried reading it but it's a complete pain without the indentation. And kind of impossible to debug python without the indentation.

import sys

itemsInBackpack = ["book", "computer", "keys", "travel mug"]

while True:
print("Would you like to:")
print("1. Add an item to the backpack?")
print("2. Check if an item is in the backpack?")
print("3. Quit")
userChoice = input()

if(userChoice == "1"):
print("What item do you want to add to the backpack?")
itemToAdd = input()

####### YOUR CODE HERE ######
#add the item to the backpack
####### YOUR CODE HERE ######

if(userChoice == "2"):
print("What item do you want to check to see if it is in the backpack?")
itemToCheck = input()

####### YOUR CODE HERE ######
#Print out if the user's item is in the backpack
####### YOUR CODE HERE ######

if(userChoice == "3"):
sys.exit()

Read the sticky!

To use the Code tag, book-end your body of code with: and

Thanks everyone for the help. but now it's repeating itself multiple times. How do I fix that?

Not trying to be a hardass, and maybe you've already done it, but you should get into the habit of looking at documentation of how to do stuff before asking for advice.

Trust me, I tried.

Why did you put it in a loop? There's no need to do that if you only want to check if a user supplied item is in a list.

I just thought of doing it like that, How would you have done it?

Because you're appending itemsInBackPack instead of ItemToAdd

You're also checking of itemInBackpack is in ItemInBackpack instead of if ItemToAdd is in

With out it.

Are you sure? I hate to be mean, but these are really basic issues.

Try looking here

docs.python.org/3/tutorial/datastructures.html

Check the indentation on you last line.

Also

if itemsInBackpack[i] == itemsInBackpack


is likely never true, because you are comparing a list element to its own list which is never true.

Use a set you fucking homo.
backpack = set()
if input in backpack:
print("Item already in backpack")
else:
backpack.add(item)

you don't need to use a set - a list is just fine for what OP's doing.

is that what a set is? a list of unique items?

Lol

if item in backpack:
print("item in backpack already")

Why would a set be good here? You are not doing anything that can't be done with a list.

Yes, if you have two equal elements then one element will be thrown away. So you get a set of unique objects

Is that a university assignment? If yes holy fuck you must be a pajeet.

Set is way faster than list for membership checking.

You can akways turn into a list if you need it.

OP is learning a new language. I doubt he cares about the subtle differences in performance when it comes to list vs. set in his homework assignment.

It's not so subtle and must be taught at the start.

Too many noobs use list for everything when s set would be better.

I'm still having trouble with my program repeating it self multiple times. I looked it up and It said it might be the indentation, however I can't seem to find the right line of code to fix.

BECAUSE YOU'RE USING A WHILE LOOP YOU RETARDED PAJEET. JUST DROP OUT OF YOUR COURSE!

1. not nice.... 2. we have to use a WHILE LOOP

What for? What do you need to print then?

This guy is right, none of you should be helping OP. This is such a basic fucking operation, and yes WHEN YOU ARE NEW AT A LANGUAGE it is difficult but that is EXACTLY the point. Read the fucking documentation. Google "loop repeating too many times Python." There are a million fucking things to do on your own before asking other people to do it for you. You will never learn doing it this way. Especially when you are clearly ignoring most of what people in this thread are saying. Do the fucking learning yourself you lazy piece of shit.

Holy shit I just saw that this is your fifth fucking week in this class. Just give up. If you are only at this level of understanding of the language after five weeks you either don't give a shit about this, or need serious tutoring/ need to devote way more time to this. Go to code academy, watch tutorials on YouTube, anything.

That's exactly what he's doing - asking people on Bulgarian carpenting board for help. But now you scared him away and he will not learn a thing.

Fuck off

I said it before and I said it again, look at the last line of your code. If something loops too much, then it's indentation. change the indentation of the loop you don't want to repeat. It repeats because it's in a loop when you don't want it to.

Likely because it's school work.

OP:

Go through this book learnpythonthehardway.org/book/

It's the easiest way to learn fast.

2 is useless you should have a list on screen all the time that shows all the stuff in the basket. You should replace it with an option that allows you to drop an item in your cart.

A set doesn't cover all circumstances.

>add banana
>add another banana
OP has now 2 bananas
unsetable if you don't add further info