我想向客户库 (dict) 添加一个新客户并将其保存在 dict 中,这样当我想添加另一个客户时,它会添加而不是替换它们
I want to add a new customer to the customers library (dict) and to save it in the dict so when I want to add another it'll add and not replace them
这是我在 LibraryCustomers.py 模块中使用的代码:
class Customer:
"""
A class that represents the Customer object
"""
def __init__(self,customer_id,customer_name,customer_city,customer_age):
"""
A function that contains all the relevant information of customers
:param customer_id: Customer's ID
:param customer_name: Customer's name
:param customer_city: Customer's city of living
:param customer_age: Customer's age'
"""
self.customer_id = customer_id
self.customer_name = customer_name
self.customer_city = customer_city
self.customer_age = customer_age
def add_new_customer(self,customer_id, customer_name, customer_city, customer_age):
"""
A function that add new customer to the Library
:param customer_id: Customer's ID'
:param customer_name: Customer's name'
:param customer_city: Customer's city'
:param customer_age: Customer's age'
"""
new_customer = {'customer id':{customer_id}, 'customer name':{customer_name}, 'customer city':{customer_city}, 'customer age':{customer_age}}
return new_customer
这是我在 main.py 中使用的代码:
添加新客户(基于输入)
customer_id_input = input("Enter customer's ID: ")
" adding customer id "
customer_name_input = input("Enter customer's name: ")
" adding customer name"
customer_city_input = input("Enter customer's city: ")
" adding customer city"
customer_age_input = input("Enter customer's age: ")
" adding customer age"
new_customers = Customer.add_new_customer(customers_library,customer_id_input,customer_name_input,customer_city_input,customer_age_input)
customers_library.update(new_customers)
# updated_customers_library = json.dumps(customers_library)
print("Added customer...")
print("Done!, Customer added successfully")
print(f"\n{customers_library}")
每次我 运行 这段代码都不会将新客户添加到 main.py
中的客户字典中
update
用于 update/insert 到现有字典中。对于新项目,使用键将其添加到字典中。但在你的情况下,如果你计划拥有多个客户,也许 customers_library
应该是客户列表,而不是字典?
class Customer:
def __init__(self, customer_id, customer_name, customer_city, customer_age):
self.customer_id = customer_id
self.customer_name = customer_name
self.customer_city = customer_city
self.customer_age = customer_age
@staticmethod
def create():
customer_id_input = input("Enter customer's ID: ")
customer_name_input = input("Enter customer's name: ")
customer_city_input = input("Enter customer's city: ")
customer_age_input = input("Enter customer's age: ")
return Customer(customer_id_input,
customer_name_input,
customer_city_input,
customer_age_input)
def __str__(self):
return f"{self.customer_id},{self.customer_name},{self.customer_city},{self.customer_age}"
class Library:
def __init__(self):
self.customers = []
def new_customer(self):
self.customers.append(Customer.create())
def __str__(self):
return '\n'.join((str(c) for c in self.customers))
customers_library = Library()
print("Added customer...")
customers_library.new_customer()
customers_library.new_customer()
print("Done!, Customer added successfully")
print(customers_library)
要使用字典而不是列表,请将 Library
class 更改为:
class Library:
def __init__(self):
self.customers = {}
def new_customer(self):
customer = Customer.create()
# Here, the key is the id, but can be anything
self.customers[customer.customer_id] = customer
def __str__(self):
return '\n'.join((str(c) for c in self.customers.values()))
这是我在 LibraryCustomers.py 模块中使用的代码:
class Customer:
"""
A class that represents the Customer object
"""
def __init__(self,customer_id,customer_name,customer_city,customer_age):
"""
A function that contains all the relevant information of customers
:param customer_id: Customer's ID
:param customer_name: Customer's name
:param customer_city: Customer's city of living
:param customer_age: Customer's age'
"""
self.customer_id = customer_id
self.customer_name = customer_name
self.customer_city = customer_city
self.customer_age = customer_age
def add_new_customer(self,customer_id, customer_name, customer_city, customer_age):
"""
A function that add new customer to the Library
:param customer_id: Customer's ID'
:param customer_name: Customer's name'
:param customer_city: Customer's city'
:param customer_age: Customer's age'
"""
new_customer = {'customer id':{customer_id}, 'customer name':{customer_name}, 'customer city':{customer_city}, 'customer age':{customer_age}}
return new_customer
这是我在 main.py 中使用的代码: 添加新客户(基于输入)
customer_id_input = input("Enter customer's ID: ")
" adding customer id "
customer_name_input = input("Enter customer's name: ")
" adding customer name"
customer_city_input = input("Enter customer's city: ")
" adding customer city"
customer_age_input = input("Enter customer's age: ")
" adding customer age"
new_customers = Customer.add_new_customer(customers_library,customer_id_input,customer_name_input,customer_city_input,customer_age_input)
customers_library.update(new_customers)
# updated_customers_library = json.dumps(customers_library)
print("Added customer...")
print("Done!, Customer added successfully")
print(f"\n{customers_library}")
每次我 运行 这段代码都不会将新客户添加到 main.py
中的客户字典中update
用于 update/insert 到现有字典中。对于新项目,使用键将其添加到字典中。但在你的情况下,如果你计划拥有多个客户,也许 customers_library
应该是客户列表,而不是字典?
class Customer:
def __init__(self, customer_id, customer_name, customer_city, customer_age):
self.customer_id = customer_id
self.customer_name = customer_name
self.customer_city = customer_city
self.customer_age = customer_age
@staticmethod
def create():
customer_id_input = input("Enter customer's ID: ")
customer_name_input = input("Enter customer's name: ")
customer_city_input = input("Enter customer's city: ")
customer_age_input = input("Enter customer's age: ")
return Customer(customer_id_input,
customer_name_input,
customer_city_input,
customer_age_input)
def __str__(self):
return f"{self.customer_id},{self.customer_name},{self.customer_city},{self.customer_age}"
class Library:
def __init__(self):
self.customers = []
def new_customer(self):
self.customers.append(Customer.create())
def __str__(self):
return '\n'.join((str(c) for c in self.customers))
customers_library = Library()
print("Added customer...")
customers_library.new_customer()
customers_library.new_customer()
print("Done!, Customer added successfully")
print(customers_library)
要使用字典而不是列表,请将 Library
class 更改为:
class Library:
def __init__(self):
self.customers = {}
def new_customer(self):
customer = Customer.create()
# Here, the key is the id, but can be anything
self.customers[customer.customer_id] = customer
def __str__(self):
return '\n'.join((str(c) for c in self.customers.values()))