2013-07-26 2 views
-1

사용자에게 제품 이름, 가격 및 수량을 입력하도록 요청하는 프로그램을 작성하려고합니다. 거기에서 모든 정보가 표 (사전?)에 추가됩니다. 또한 ID 번호는 생성 된 모든 새 항목에 지정되어야합니다. 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 
+3

ID 번호에 대한 요구 사항을 자세히 설명해야합니다. 또한 ID 번호를 생성하기 위해 시도한 것을 나열해야합니다. – Amber

+0

몇 개의 ID 번호가 있습니까? –

답변

0

당신은

>>> from uuid import uuid4 
>>> uuid4().clock_seq 
7972L 
>>> uuid4().clock_seq 
11807L 
>>> uuid4().clock_seq 
15487L 
+0

제품 번호에 과도하게 과도한 부담이 될 것이라는 느낌이 들었습니다 ... 매장이나 다른 곳에서 키를 입력하고 싶지는 않을 것입니다.) –

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) 
0

는 당신이 필요합니까 고유 ID를 생성 할 uuid4를 사용할 수 있습니다 정보를 검색 할 수 있습니까? 여러 사전 키를 고려하거나 튜플 또는 목록을 사용하려고 할 수 있습니다.

특히 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() 
+0

예, 정보를 검색해야합니다. 또한 모듈을 가져 오지 않고이 작업을 수행 할 수있는 방법이 있습니까? – user2581724

+0

아니요, 임의의 ID를 만들려면 모듈을 가져와야합니다. nonrandom ID가 괜찮 으면, 그럴 필요가 없습니다. – AppliedNumbers

관련 문제