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?
>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 saiditemsInBackpack = ['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)))
>>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 beitemsInBackpack.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)))