0
x = input('Check item: ')
x = x.split()
f = open('packed.txt')
a = []
for i in f:
    b = i[:-1]
while x: 
  if b in x:
      print("It's packed.")
  else:
      print("You haven't packed it yet!")
  x = input('Check item: ')

packed.txt

sunscreen, towel, sunglasses, swimming goggles, sandals, water bottle

The B element can not be found in x for some reason.

And it says I haven't packed it. Can you find what I did wrong??

YC327
  • 11
  • 4
  • 3
    Why are you doing this - `b = i[:-1]` , what do you think it does? Also please show us how your inputs/outputs would look like. – Anand S Kumar Aug 25 '15 at 10:26
  • `b` is a list.So you are saying if `list` in `list`? iterate through both if you want to compare – vks Aug 25 '15 at 10:27
  • Check item: towel sunscreen towel sunglasses swimming goggles sandals water bottle You haven't packed it yet! Check item: – YC327 Aug 25 '15 at 10:28
  • What I have to do is go through packed.txt file and check whether or not my input is same as my packed.txt list. – YC327 Aug 25 '15 at 10:31

3 Answers3

0

Change

if b in x:

with

if x[0] in b:

And add

x = x.split()

At the end of while loop.

Here the complete version:

x = input('Check item: ')
x = x.split()
f = open('packed.txt')
a = []
for i in f:
    b = i[:-1]


while x:
  print "B ",b
  print "X ",x
  if x[0] in b:
      print("It's packed.")
  else:
      print("You haven't packed it yet!")
  x = input('Check item: ')
  x = x.split()

Now, it works! Hope I helped you!

vathek
  • 531
  • 2
  • 9
  • I try and it work for me. The packed.txt file has one line with comma separated elements. When app prompt for item, i write it into double quoted: "towel" or "sunglasses" for example. I use python 2.7.3 – vathek Aug 25 '15 at 10:45
0

First of all, you should not make a list out of x. so just left line with input(). Second. In for i in f: i is line, not a word. If you want to read words from line you have to do

file = open('filename.txt')
items = file.readline().split(', ')

But it's good practice to open files with 'with':

with open('packed.txt', 'r') as f:
    items = f.readline().split(', ')

Second argument in open() is 'r', this is the way to tell python we want to open the file in read mode. If you want to read about other modes go here

If you really want to use classic way remember about close the file with 'file.close()'

In your case because items in your file are separated by ','. we are using split(',') so now we have list of your items.

Now you just have to check if item that you entered is on the list.

if item in packed_items:
    print("It's packed.")

after all code should looks more/less similar to this one:

item = input('Check item: ')
with open('packed.txt', 'r') as f:
    items = f.readline().split(', ')
while item:
    if item in items:
        print("It's packed.")
    else:
        print("You haven't packed it yet!")
    item = input('Check item: ')

If you want to check multiply items at once add this after first line:

item = item.split()

And then change:

if item in items:
    print("It's packed")
else:
    print("You haven't packed it yet!")

to:

for check in item:
    if check in items:
        print(check + ' is packed')
    else:
        print(check + ' is not packed yet')

And add this line 'item = item.split()' at the end of file after but within the while loop: item = input('Check item: ')

Sorry for any misspelling :). Hope its helpful :)

Quba
  • 4,776
  • 7
  • 34
  • 60
  • For some reason it is keep on saying 'You haven't packed it yet!'. – YC327 Aug 25 '15 at 11:23
  • Ok. I see the mistake. It should be items = f.readline.split(', ') instead of f.readline.split(',') Let me know if its working now. – Quba Aug 25 '15 at 14:11
0

Very small change to code posted by vathek . Just change 'input' to 'raw_input'. There is difference between input and raw_input. 'raw_input' returns string whereas 'input' tries run the value entered as python expression. Please refer to post for more info : What's the difference between raw_input() and input() in python3.x? So, if you are using 'input' then you need to enter value on commandline with single or double quotes. If you use raw_input, then there is no need to use quotes.

x = raw_input('Check item: ')
x = x.split()
f = open('packed.txt')
a = []
for i in f:
    b = i[:-1]


while x:
  print "B ",b
  print "X ",x
  if x[0] in b:
      print("It's packed.")
  else:
      print("You haven't packed it yet!")
  x = raw_input('Check item: ')
  x = x.split()

Output :

C:\Users\Administrator\Desktop>python demo.py
Check item: towel
B  sunscreen, towel, sunglasses, swimming goggles, sandals, water bottl
X  ['towel']
It's packed.
Check item: sandals
B  sunscreen, towel, sunglasses, swimming goggles, sandals, water bottl
X  ['sandals']
It's packed.
Check item: coke
B  sunscreen, towel, sunglasses, swimming goggles, sandals, water bottl
X  ['coke']
You haven't packed it yet!
Check item: sunscreen
B  sunscreen, towel, sunglasses, swimming goggles, sandals, water bottl
X  ['sunscreen']
It's packed.
Community
  • 1
  • 1
Dinesh Pundkar
  • 4,160
  • 1
  • 23
  • 37