[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

python help

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 39
Thread images: 4

File: python.png (80KB, 1000x1000px) Image search: [Google]
python.png
80KB, 1000x1000px
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()
>>
>>57833522
itemsInBackpack.append(itemToAdd)
>>
>>57833522
#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")
>>
>>57833522
Format your code!
>>
>>57833522
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)))
>>
>>57833522
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()
>>
File: 002 - uBt9mRA.jpg (35KB, 288x604px) Image search: [Google]
002 - uBt9mRA.jpg
35KB, 288x604px
>>57833800
Read the sticky!
>>
>>57833800
To use the Code tag, book-end your body of code with: and
>>
File: help.png (130KB, 1920x1080px) Image search: [Google]
help.png
130KB, 1920x1080px
Thanks everyone for the help. but now it's repeating itself multiple times. How do I fix that?
>>
>>57833522
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.
>>
>>57833868
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?
>>
>>57833868
Because you're appending itemsInBackPack instead of ItemToAdd

You're also checking of itemInBackpack is in ItemInBackpack instead of if ItemToAdd is in
>>
>>57833901
With out it.
>>
>>57833880
Are you sure? I hate to be mean, but these are really basic issues.

Try looking here

https://docs.python.org/3/tutorial/datastructures.html
>>
>>57833868
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.
>>
>>57833868
Use a set you fucking homo.
backpack = set()
if input in backpack:
print("Item already in backpack")
else:
backpack.add(item)
>>
>>57834009
you don't need to use a set - a list is just fine for what OP's doing.
>>
>>57834009
is that what a set is? a list of unique items?
>>
>>57833579
Lol

if item in backpack:
print("item in backpack already")
>>
>>57834009
Why would a set be good here? You are not doing anything that can't be done with a list.
>>
>>57834058
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.
>>
>>57834060
Set is way faster than list for membership checking.

You can akways turn into a list if you need it.
>>
File: 020 - RIlmT.jpg (30KB, 216x259px) Image search: [Google]
020 - RIlmT.jpg
30KB, 216x259px
>>57834083
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.
>>
>>57834097

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.
>>
>>57834175
>>57834175
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
>>
>>57834262
What for? What do you need to print then?
>>
>>57834175
>>57834212
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.
>>
>>57834384
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.
>>
>>57834429
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.
>>
>>57834212
Fuck off

>>57834175
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.

>>57834355
Likely because it's school work.
>>
OP:

Go through this book https://learnpythonthehardway.org/book/

It's the easiest way to learn fast.
>>
>>57833522
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.
>>
>>57834083
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
Thread posts: 39
Thread images: 4


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.