Python中的类和对象

时间:2016-11-30 04:43:17

标签: python python-3.x

我正在运行一个简单的在线购物车程序,当我尝试运行它时,结果结果为空白。我理解关于类和对象的概念,但我真的需要帮助。它应该看起来像这样:

if (in_array(get_the_title(), $category)) {
                        $tripSelect[] = get_the_title() . '-selected';
                    } else {
                        $tripSelect[] = get_the_title();
                    }

                } else {
                    $tripSelect[] = get_the_title();
                }

            endwhile;
        }
    }
    echo json_encode($query);

这是我到目前为止所写的内容:

Item 1
Enter the item name: Chocolate Chips
Enter the item price: 3
Enter the item quantity: 1

Item 2
Enter the item name: Bottled Water
Enter the item price: 1
Enter the item quantity: 10

TOTAL COST
Chocolate Chips 1 @ $3 = $3
Bottled Water 10 @ $1 = $10

Total: $13

我做错了什么?

1 个答案:

答案 0 :(得分:3)

您的print_item_cost方法存在一些问题,应该是这样的:

def print_item_cost(self):
    total = self.item_quantity * self.item_price
    print('%s %d @ $%f = $%f' % (self.item_name, self.item_quantity, self.item_price, total))

您可以引用这样的类属性:self.attr