3 Replies - 494 Views - Last Post: 17 April 2016 - 09:45 PM Rate Topic: -----

#1 farschüler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-April 16

For loop only works with strings inside lists

Posted 17 April 2016 - 04:04 PM

Howdy! I'm new, but not a student in the school sense of things. I am learning though - or attempting to - through free online sites. I'm going beyond their lesson though, and hitting a brick wall. I'm sure the answer is out there, but maybe I'm just searching wrong.

I'm creating dictionaries for price and stock, then taking input in a function amount_due to tie what I've learned together. Unfortunately, it only works with lists. When I put a string in without brackets, the for loop runs for each character inside of the string.

# Creating price list

prices = {
    'banana' : 4,
    'apple' : 2,
    'orange' : 1.5,
    'pear' : 3
}

# Creating stock list

stock = {
    'banana' : 6,
    'apple' : 0,
    'orange' : 32,
    'pear' : 15
}


groceries = ['banana', 'apple']

def amount_due(food):
    total = 0
    for x in food:
        if stock[x] > 0:
            total += prices[x] # Adds price of food to total price
            print 'There are %s %ss in stock. Your %s costs $%s, which brings your total to $%s' % (stock[x], x, x, prices[x], total)
            stock[x] -= 1 # Subtracts item from stock 
        else:
            print 'Sorry, %ss are out of stock' % x
    return total

print amount_due(groceries) # Works
print amount_due(['orange']) # Works
print amount_due('banana') # Error



Error code I get is:
Traceback (most recent call last):
  File "store_cut.py", line 35, in <module>
    print amount_due('banana')
  File "store_cut.py", line 25, in amount_due
    if stock[x] > 0:
KeyError: 'b'



Thanks so much for any help or input you have!!!

Is This A Good Question/Topic? 0
  • +

Replies To: For loop only works with strings inside lists

#2 astonecipher  Icon User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2517
  • View blog
  • Posts: 10,091
  • Joined: 03-December 12

Re: For loop only works with strings inside lists

Posted 17 April 2016 - 05:25 PM

Determine which type the parameter should expect. If it is going to be a dict, it should not also accept a string.
Was This Post Helpful? 1
  • +
  • -

#3 farschüler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-April 16

Re: For loop only works with strings inside lists

Posted 17 April 2016 - 06:33 PM

Thank you. This is my first (real) programming language, so I'm not only learning python, but also attempting to learn programming in general.

While this probably isn't ideal, I believe this would be one way to attack the problem:

def add_item():
    print 'What would you like to add?'
    new = str(raw_input())
    new = new.lower()
    for key in prices:
        if new == key:
            groceries.append(new)
    return groceries

amount_due(add_item())



Thanks again for your input! I know that there's an issue with the above code for adding an else statement. I'm just not sure how to solve it yet :)
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2517
  • View blog
  • Posts: 10,091
  • Joined: 03-December 12

Re: For loop only works with strings inside lists

Posted 17 April 2016 - 09:45 PM

Function calls should be within functions/ methods, not the arguments declaration.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1