-1

我正在尝试编写一个程序,要求用户输入产品名称、价格和数量。从那里,所有信息都将被添加到一个表中(字典?)此外,必须为所有创建的新项目分配一个 ID 号。我对身份证号码段感到困惑。

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
4

3 回答 3

0

您可以使用uuid4创建唯一 ID

>>> from uuid import uuid4
>>> uuid4().clock_seq
7972L
>>> uuid4().clock_seq
11807L
>>> uuid4().clock_seq
15487L
于 2013-07-26T18:54:12.080 回答
0

您需要能够检索信息吗?您可能会考虑使用多个字典键,或者只想使用元组或列表。

您几乎肯定需要导入一些东西来创建 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()
于 2013-07-27T01:17:08.230 回答
0
items = {}
import hashlib
product = "dinosaur"
price = 10.99
quantity = 20
uid = hashlib.md5(product).hexdigest() #is one way but you probably don't
# need a hash if you just want simple number id's: a much easier way is 
otheruid = len(items) # just have the next item be the UID
items[otheruid] = (product, price, quantity) 
于 2013-07-26T18:58:22.387 回答