为什么我将变量名作为输出而不是其值?

时间:2019-09-26 01:40:00

标签: python

我有Order个对象的列表。每个对象都具有以下属性user_id, product_id, quantity。 我正在使用下面的代码遍历列表并获取每个对象的属性值。

for order in Orders:
   print(order.user_id)

这将打印预期的输出。但是,我需要使用每个user_id中的order来从User对象的列表中获取User对象的属性。为此,我必须将order.user_id转换为整数才能像users[order.user_id].first_name一样使用它,但是当我将order.user_id转换为int时,出现错误:

ValueError: invalid literal for int() with base 10: 'user_id'

因此,当我使用print(order.user_id)时,我得到了值,但是当我使用print(int(order.user_id))时,我得到了"user_id"作为不能转换为int的输出。我在做什么错了?

我有以下课程:

class User(object):
    def __init__(self, user_id, first_name, last_name, email, gender):
        self.id = user_id
        self.first_name = first_name
        self.last_name = last_name
        self.email = email
        self.gender = gender


class Product(object):
    def __init__(self, product_id, description, stock, cost):
        self.id = product_id
        self.description = description
        self.stock = stock
        self.cost = cost


class Order(object):
    def __init__(self, product_id, user_id, quantity):
        self.product_id = product_id
        self.user_id = user_id
        self.quantity = quantity

这是我填充列表的方式:

users = []
products = []
orders = []

"""read all the products"""

cur = db.cursor()
cur.execute('SELECT* FROM products')
product_rows = cur.fetchall()
for product in product_rows:
    products.append(make_product(product[0], product[1], product[2], product[3]))

"""read all the users"""

cur = db.cursor()
cur.execute('SELECT* FROM users')
user_rows = cur.fetchall()
for user in user_rows:
    users.append(make_user(user[0], user[1], user[2], user[3], user[4]))

"""read all the orders"""

cur = db.cursor()
cur.execute('SELECT* FROM orders')
order_rows = cur.fetchall()
for order in order_rows:
    orders.append(make_order(order[0], order[1], order[2]))

'make'方法:

def make_user(id, first_name, last_name, email, gender):
    user = User(id, first_name, last_name, email, gender)
    return user


def make_product(id, description, stock, cost):
    product = Product(id, description, stock, cost)
    return product


def make_order(product_id, user_id, quantity):
    order = Order(product_id, user_id, quantity)
    return order

我使用时的输出:

    for order in orders:
        string = order.user_id
        print(string)

我得到输出:

80
10
25
88
48
41
93

当我使用     对于订单中的订单:         字符串= order.user_id         print(int(string))

我得到以下输出:

ValueError: invalid literal for int() with base 10: 'user_id'

我实际上正在尝试做的事情:

for order in orders:
    user_id = int(order.user_id)
    first_name = users[user_id].first_name
    last_name = users[user_id].last_name
    product_id = int(order.product_id)
    product_description = products[product_id].description
    order_quantity = order.quantity
    unit_price = products[product_id].cost
    total_price = unit_price * order_quantity

    line = user_id + "," + first_name + "," + last_name + "," + product_id + "," + \
    product_description + "," + order_quantity + "," + unit_price + "," + str(total_price)

很抱歉,这是一个非常基本的问题。

0 个答案:

没有答案