Python help

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)))

Other urls found in this thread:

automatetheboringstuff.com/
twitter.com/SFWRedditImages

Your while loop isn't ending?

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?

>his while statement doesn't have a condition for it to stop

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?

The problem is your indentation. There is nothing in the loops, how you get it to run at all is a miracle.

first of all, do like 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;

as someone posted before...

use code blocks ([ c o d e ] + [ / c o d e ], without spaces)

Don't spoonfeed him when it's literally in the sticky

Put your code inside

[/code.]
(Just remove the period for it to work)

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)))

lol, didn't think it would work. Fuck me.

where do you learn python?

it is a good source
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)

Thanks for pointing it out! I missed that whilst copying from OP.

Why are you appending the list back on to itself? Should enter you be appending it with thee user input variable?

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"

I don't get the problem...


Is it appended like this user did? Because THAT works flawlessly apart from the "itemToAdd"-bug

indentated, not appended

Because it was in a loop. Do what's given to you in

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)))