-2

I am learning about records in my computing class, I understand that they are like a record in a data base. I was told to use namedtuples for this. But my code isn't working. I get a "keyword can't be expression" error when running it.

from collections import namedtuple

product = namedtuple("Product", "Product Name", "Stock Number", "Price", "Purchased")

def Setup(product):
    shoppingBasket = []
    shoppingBasket[0] = product("Product Name" = "USB cable", "Stock Number" = 624, "Price" = 1.74, "Purchased" = False)
    shoppingBasket[1] = product("Product Name" = "HDMI adaptor", "Stock Number" = 523, "Price" = 5.00, "Purchased" = False)
    shoppingBasket[2] = product("Product Name" = "DVD-RW pack", "Stock Number" = 124, "Price" = 10.99, "Purchased" = False)

    return shoppingBasket



def Main():
    shoppingBasket = Setup(product)
    cart = []
    for item in shoppingBasket:
        addItem = raw_input("Would you like to buy this item? ")
        if addItem == "yes":
            shoppingBasket[item].Purchased = True
            cart.append(shoppingBasket)
    total = calculate(cart)


def calculate(cart):
    total = 0 
    for item in cart:
        total += item.Price
    return total 

It says the error is on line 7 where I set shoppingBasket[0] to what I think is a record. What is wrong here?

Bruce W
  • 35
  • 6
  • 1
    Isn't it the purpose of your Advanced Higher course to teach you these things? – mkrieger1 Jun 24 '15 at 14:53
  • Maybe you can also learn something from the Wikipedia articles about [these](https://en.wikipedia.org/wiki/Record_%28computer_science%29) [topics](https://en.wikipedia.org/wiki/Linked_list). – mkrieger1 Jun 24 '15 at 14:55

1 Answers1

0

There are a number of errors in your code. In the second line, the syntax for the named tuple should be

product = namedtuple("Product", "ProductName StockNumber Price Purchased")

And you cannot say shoppingBasket[0], because the list has no element at that index. You will get a list index out of range error. Also, the error 'keyword can't be an expression' is due to wrong syntax. The correct statement is

shoppingBasket.append(product( "USB cable", 624,  1.74, False))

Then in the for loop, you cannot use shoppingBasket[item], since the item iterating through the loop is not the list index. You should use range and len,

for item in range(len(shoppingBasket)):
    print shoppingbasket[item]
    addItem = raw_input("Would you like to buy this item? ")
    if addItem == "yes":
        shoppingBasket[item].Purchased = True
        cart.append(shoppingBasket)
total = calculate(cart)

And finally, the statement

shoppingBasket[item].Purchased = True

will not work since namedtuples are immutable, attributes cannot be set. So using namedtuples doesn't work if that's your code. You have to use recordtype, which is not native to Python.

from recordtype import recordtype
product = recordtype("Product", "ProductName StockNumber Price Purchased")

Make these changes to your code and try it out.

  • No, sorry this isn't what I meant. I really meant are records dictionaries and are linked lists just looping through a list. – Bruce W Jun 24 '15 at 14:26
  • Okay, I read the Wikipedia pages on records and linked lists. They seem like data structures that can be implemented using lists and dictionaries. – koushik-ksv Jun 24 '15 at 15:05
  • The following links have the implementations. linked lists - [link](http://stackoverflow.com/questions/280243/python-linked-list) , records - [link](http://stackoverflow.com/questions/5227839/why-python-does-not-support-record-type-i-e-mutable-namedtuple) – koushik-ksv Jun 24 '15 at 15:08
  • When I would google linked lists and records those sorts of things would appear the problem is I don't really understand what's going on there. – Bruce W Jun 24 '15 at 15:54
  • Try to be more specific about what you don't understand and edit your question or better post a new one. More people will be looking at your question then. – koushik-ksv Jun 25 '15 at 12:19
  • I have been doing more research and now understand linked lists, and my teacher talked to others and found that I need to use namedtuples for records so I have edited it, cause my code won't work. – Bruce W Jun 26 '15 at 09:49
  • I suggest you try to understand this on your own, using any resource online. Sorry, but I don't know about named tuples. I will get back to you after I study named tuples – koushik-ksv Jun 26 '15 at 13:43
  • I am trying to find some solution but so far not much. – Bruce W Jun 26 '15 at 21:02
  • You have to try that on your own. Finding a solution will not help you that much. Unless you code on your own, the purpose of your course will not be met. I can probably find a solution and post the code here, but I hope you will post your own code after trying for a while. – koushik-ksv Jun 27 '15 at 15:21
  • I just noticed now that your answer was edited and it did help, thank you. My teacher told me to use namedtuples when I thought using an array of dictionaries would work better. – Bruce W Jul 01 '15 at 09:14
  • Well, I hope you have solved it. Follow your teacher, but you can always experiment with code. – koushik-ksv Jul 03 '15 at 18:06