Python:添加一个条目?
我正在尝试编写一个程序,要求用户输入产品名称、价格和数量。从那里,所有信息都将添加到一个表(字典?)中。此外,必须为创建的所有新项目分配一个ID号。我对身份证号码部分感到困惑Python:添加一个条目?,python,dictionary,Python,Dictionary,我正在尝试编写一个程序,要求用户输入产品名称、价格和数量。从那里,所有信息都将添加到一个表(字典?)中。此外,必须为创建的所有新项目分配一个ID号。我对身份证号码部分感到困惑 items = {} product = str(input("Enter the name of a product: ")) price = int(input("Enter a price: ")) quantity = int(input("Enter a quantity: ")) #Assign uniqu
items = {}
product = str(input("Enter the name of a product: "))
price = int(input("Enter a price: "))
quantity = int(input("Enter a quantity: "))
#Assign unique ID number.
我试图以下面的结果为例:
ID#70271, shoes. 7 available, at $77.00
您可以使用创建唯一id
>>> from uuid import uuid4
>>> uuid4().clock_seq
7972L
>>> uuid4().clock_seq
11807L
>>> uuid4().clock_seq
15487L
您需要能够检索信息吗?您可能会考虑多个字典键,或者只想使用元组或列表。 您几乎肯定需要导入一些东西来创建ID,特别是如果它是随机的。下面是一些代码,用于将该信息添加到已存在的填充或空列表中
def storesInfo(product,quantity,price,listInfo): #takes list as a variable
import random
ID = random.randrange(0,10000)
listInfo.append([product,price,quantity,ID])
return('ID%s, %s. %f available, at %f.'%(ID, product, quantity, price),listInfo)
def main():
product = str(input("Enter the name of a product: "))
price = int(input("Enter a price: "))
quantity = int(input("Enter a quantity: "))
listA = [[2983, 'socks',32,65.23],[9291,'gloves',98,29.00]]
print(storesInfo(product,quantity,price,listA))
main()
你必须详细说明你的身份证号码的要求。您还应该列出您在生成ID号方面所做的尝试。我有一种感觉,对于产品号来说,这将是一种极大的矫枉过正。。。当然不想在商店或其他地方输入这些信息;)是的,我需要检索信息。另外,有没有一种方法可以在不导入任何模块的情况下执行此操作?没有,为了创建随机ID,必须导入模块。如果非随机ID没有问题,那么您就不需要它。
def storesInfo(product,quantity,price,listInfo): #takes list as a variable
import random
ID = random.randrange(0,10000)
listInfo.append([product,price,quantity,ID])
return('ID%s, %s. %f available, at %f.'%(ID, product, quantity, price),listInfo)
def main():
product = str(input("Enter the name of a product: "))
price = int(input("Enter a price: "))
quantity = int(input("Enter a quantity: "))
listA = [[2983, 'socks',32,65.23],[9291,'gloves',98,29.00]]
print(storesInfo(product,quantity,price,listA))
main()