【发布时间】:2022-01-04 16:49:02
【问题描述】:
我想我已经掌握了基本概念,但我无法真正让它正常工作。我想创建 5 个具有自己属性的不同对象,每个对象 3 个,但它们必须是输入。它们必须使用另一个函数创建,然后返回它们以显示用户并将它们放入列表中。这就是我所拥有的,但我不知道如何使它工作:
class SmartPhone:
def __init__(self, manufacturer, model, cost):
self.manufacturer = manufacturer
self.model = model
self.cost = cost
manufacturer = input("Εισήγαγε τον κατασευαστή: ")
model = input("Εισήγαγε το μοντέλο: ")
cost = input("Εισήγαγε την λιανική τιμή: ")
def smart_phones(manufacturer, model, cost):
smartPhones = []
phone = SmartPhone(manufacturer, model, cost)
smartPhones.append(phone)
return "Smartphone: " + str(phone), smartPhones
smart_phones = smart_phones(manufacturer, model, cost)
for i in range(0, 5):
print(smart_phones)
问题基本上在于 smart_phones 功能。
【问题讨论】:
-
请注意,您正在用它返回的元组覆盖函数
smart_phones。