[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: 26
Thread images: 3

File: python.png (80KB, 1000x1000px) Image search: [Google]
python.png
80KB, 1000x1000px
Help, My python code is giving the out put multiple times. not too sure what is wrong with it.

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()
itemsInBackpack.append(itemsInBackpack)
print('you have just added ' + itemToAdd + ' to your backpack')

if(userChoice == "2"):
print("What item do you want to check to see if it is in the backpack?")
itemToCheck = input()
for i in range(len(itemsInBackpack)):
if itemToCheck in itemsInBackpack:
print("Already in backpack")
else:
print('nope')
print('Items in backpack:{0}'.format(', '.join(itemsInBackpack)))
>>
>>57839234
Your while loop isn't ending?
>>
>>57839234
1) install gentoo
2) use the stupid question thread / daily programming thread
3) use code blocks ([ c o d e ] + [ / c o d e ], without spaces)
>>
is this the no code tags meme?
>>
File: 1441158724065.gif (3MB, 286x258px) Image search: [Google]
1441158724065.gif
3MB, 286x258px
>his while statement doesn't have a condition for it to stop
>>
>>57839283
>>57839314
i couldnt figure it out too, guess im more retarded than OP
>>
Can someone just ban you fuckers? READ THE STICKY when you are posting code.

Are you just trolling because it is literally impossible to see what is wrong with your code when you post without code blocks? This is likely easy to fix, but you don't have code blocks. Are you deliberately doing this?

In that case, nice meme. It's pretty infuriating
>>
How does one post with code block?
>>
>>57839234
The problem is your indentation. There is nothing in the loops, how you get it to run at all is a miracle.
>>
>>57839283
first of all, do like >>57839305 said, and put your code inside the code tags.
Second of all, your while loop needs to become false at some point. So you could make it false during the end of each condition. Something like this:


if(poop peepee)
//do your shit;
variable = false;

>>
>>57839359
as someone posted before...

use code blocks ([ c o d e ] + [ / c o d e ], without spaces)
>>
>>57839372
>>57839305
Don't spoonfeed him when it's literally in the sticky
>>
>>57839359
Put your code inside
 

[/code.]
(Just remove the period for it to work)
>>
>>57839234
Have you not learnt anything from previous thread? You NEED to put your code inside code tags, otherwise whitespace is not preserved, and it so happens, that whitespace matters in python

That being said

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()
itemsInBackpack.append(itemsInBackpack)
print('you have just added ' + itemToAdd + ' to your backpack')

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

if itemToCheck in itemsInBackpack:
print('{0} is in backpack'.format(itemToCheck))
else:
print('{0} is not in backpack'.format(itemToCheck))

if userChoice == '3':
break

print('Items in backpack: {0}'.format(', '.join(itemsInBackpack)))
>>
File: XC7b9Vb.jpg (66KB, 668x720px) Image search: [Google]
XC7b9Vb.jpg
66KB, 668x720px
>>57839388
lol, didn't think it would work. Fuck me.
>>
where do you learn python?

it is a good source
https://automatetheboringstuff.com/
>>
Since whitespace is important in python it's basically impossible to read this without code blocks.
>>
    if userChoice == '1':
print('What item do you want to add to the backpack?')
itemToAdd = input()
itemsInBackpack.append(itemsInBackpack)
print('you have just added ' + itemToAdd + ' to your backpack')


You are not adding an item to the list...

itemsInBackpack.append(itemsInBackpack)


you are adding the whole original list into itself.

it should be

itemsInBackpack.append(itemToAdd)
>>
>>57839505
Thanks for pointing it out! I missed that whilst copying from OP.
>>
>>57839405
Why are you appending the list back on to itself? Should enter you be appending it with thee user input variable?
>>
>>57839505
>>57839520
I don't see any problems after doing this.
>>
I got a problem when I was checking to see if an item was in the back pack, the out put was

"What item do you want to check to see if it is in the backpack?
book
Already in backpack
Already in backpack
Already in backpack
Already in backpack"
>>
>>57839567
I don't get the problem...


Is it appended like this anon did? >>57839405


Because THAT works flawlessly apart from the "itemToAdd"-bug
>>
>>57839594
indentated, not appended
>>
>>57839567
Because it was in a loop. Do what's given to you in >>57839405
>>
>>57839567
you don`t need for loop to check the list, it prints 7 times same thing

 
print("What item do you want to check to see if it is in the backpack?")
itemToCheck = input()
if itemToCheck in itemsInBackpack:
print("Already in backpack")
else:
print('nope')
print('Items in backpack:{0}'.format(', '.join(itemsInBackpack)))
Thread posts: 26
Thread images: 3


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