A DAY AT THE SUPERMARKET...How can imtake a list of multiple strings as an input for LIST food


#1

my code gives the following error

code:


stock = {
  "banana": 6,
  "apple": 0,
  "orange": 32,
  "pear": 15
}
    
prices = {
  "banana": 4,
  "apple": 2,
  "orange": 1.5,
  "pear": 3
}

def compute_bill(food):
  total=0
  for i in food:
    if stock[i]>0:
     total+= prices[i]
     stock[i]-=1
    else:
      print i +" out of stock"
  return total
  

z=[]
food=[]

def getList():
  z= raw_input('Enter 4 comma separated grocery items')
  z= z.split(',')
  return z


getList()
food.append([z])
print compute_bill(food)

error

Enter 4 comma separated grocery itemsapple
Traceback (most recent call last):
  File "test.py", line 39, in <module>
    print compute_bill(food)
  File "test.py", line 20, in compute_bill
    if stock[i]>0:
TypeError: unhashable type: 'list'



#2

why would you want to append() to food? you can just get the list returned by getList function, and pass that as argument to compute_bill function call


#3

yeah sorry got it …

thnx appreciate your help

modification


shopList= [] 
maxLengthList = 4
while len(shopList) < maxLengthList:
    item =raw_input("Enter your Item to the List: ")
    shopList.append(item)
    print shopList
print "That's your Shopping List"
print shopList

#4

But do you have it in a separate function? because that would be really good


#5

no…i have only one function to calculate the billl,the code


shopList= [] 
maxLengthList = 4
while len(shopList) < maxLengthList:
    item =raw_input("Enter your Item to the List: ")
    shopList.append(item)
    print shopList
print "That's your Shopping List"
print shopList

is to only initialize the list from the user input


print "WELCOME TO MONTY GROCERY STORE"

print "THIS IS WAHT WE HAVE IN STORE FOR YOU TODAY"

stock = {
  "banana": 6,
  "apple": 0,
  "orange": 32,
  "pear": 15
}
    
prices = {
  "banana": 4,
  "apple": 2,
  "orange": 1.5,
  "pear": 3
}
print "STOCK",stock


print "PRICES", prices
m=""


def compute_bill(shopList):
  total=0
  for i in shopList:
    if stock[i]>0:
     total+= prices[i]
     stock[i]-=1
    else:
      print i + "OUT OF STOCK"
  return total
  

shopList= [] 
maxLengthList = 4
while len(shopList) < maxLengthList:
    item =raw_input("Enter your Item to the List: ")
    shopList.append(item)
    print shopList
print "That's your Shopping List"
print shopList


print "YOUR TOTAL BIlL IS " , compute_bill(shopList) 


thnaks again


#6

but having a separate function to get the user input was a better design, why did you let it go?

the only thing you needed to understand was that we can store the returned result in a variable:

food = getList()

And then all would have been good, you could have passed food as argument to compute_bill


#7

would be …


def getlist(food)
maxlenlist=4
while len(food)<len(maxlenlist)

item=raw_input("enter xyz")
food.append(item)
return food

am i right ?

thnx


#8

i would then place food = [] inside the getlist function, and why does the function have food as a parameter all of a sudden?


#9

my bad …


def getlist()
food=[]
maxlenlist=4
while len(food)<len(maxlenlist)

item=raw_input("enter xyz")
food.append(item)
return food

this good?


#10

maxlenlist is an integer, so not sure why you would do len(maxlenlist).


#11

sorry im a beginner…

pls excuse me …`

def getlist()
food=[]
maxlenlist=4
while len(food)<maxlenlist

item=raw_input("enter xyz")
food.append(item)
return food


#12

That looks about right :slight_smile: Does it work?


#13

thank it worked ,appreciate it

caould i ask you another question pls …

how can i print an element of a list…the list being is in a dictionary

eg

animal = {
  "name": "Mr. Fluffles",
  "sounds": ["meow", "purr"]
}
print animal["name"]

if say i wanted to print only meow ???


#14

this is the general syntax:

dictionary[key][index]

this assumes the value belonging to key in dictionary is a list or a string (or something that can be accessed by index), otherwise you get an error


#15

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.