字典代码只是一遍又一遍地循环

时间:2016-12-20 17:58:32

标签: python loops dictionary

我有一个文本文件,我正在为一个名为'Bodn's Superstore'的模拟商店设计。 我设计了一个.txt数据库。

iPhone
28273139
5.50
Book
81413852
1.50
Charger
62863152
3.00
Plug
25537398
4.50

您可以看到它遵循格式

Name
Code
Price

我编写的代码旨在将数据库转换为字典,客户可以选择他们购买的产品数量。然后,他们将8位数代码输入计算机,IDLE用于计算产品名称。

代码显示如下。它首先验证代码以确保它长度为8个字符。 (Python 3.4.2)

database = open("SODatabase.txt", "r")
list = {}
for line in database:
    key = line.strip()
    code = next(database).strip()
    price = next(database).strip()
    # next(database).strip() # if using python 3.x
    list[key]=code,price

numberItems=int(input('State the quantity required. >> '))
with open('SODatabase.txt','r') as searchfile:
    for line in searchfile:
        while True:
                userCode=input('What product would you like? Enter the product code >> ')
                try:
                    if len(str(userCode))!=8:
                        raise ValueError()
                    userCode=int(userCode)
                except ValueError:
                    print('The code must be 8 characters long.')
                else:
                    for key, value in list.items():
                        if userCode==value:
                            print (key)

现在,代码的验证工作正常。 比如说,我想购买1部iPhone。 这是主窗口中显示的内容。

State the quantity required. >> 1
What product would you like? Enter the product code >> 2827313
The code must be 8 characters long.
What product would you like? Enter the product code >> 28273139
What product would you like? Enter the product code >> 28273139
What product would you like? Enter the product code >>

依此类推。 代码根本不会向后工作以找到字典的键并打印它,即“iPhone”。 在我说明我的数量和产品代码之后,我希望收到名称“iPhone”,但是我的Python文件将无法通过字典返回,以找到与我给出的产品代码(值)对应的密钥

2 个答案:

答案 0 :(得分:1)

我不明白为什么需要for line in searchfile;似乎是一个复制错误。

无论如何,userCode永远不等于value,因为value是一个元组;但它可以等于保存代码的value[0]

那怎么样?

while True:
    userCode = input('What product would you like? Enter the product code >> ')
    try:
        if len(str(userCode))!=8:
            raise ValueError()
        userCode = int(userCode)
    except ValueError:
        print('The code must be 8 characters long.')
    else:
        for key, value in list.items():
            if userCode == value[0]:
                print (key)

答案 1 :(得分:1)

database = open("SODatabase.txt", "r")
list = {}
for line in database:
    key = line.strip()
    code = int(next(database).strip())
    price = next(database).strip()
    # next(database).strip() # if using python 3.x
    list[key] = code, price

while True:
    userCode = input('What product would you like? Enter the product code >> ')
    if userCode == "":
        break
    try:
        if len(str(userCode)) != 8:
            raise ValueError()
        userCode = int(userCode)
    except ValueError:
        print('The code must be 8 characters long.')
    else:
        for key, value in list.items():
            if userCode == value[0]:  # code is stored as 1st element of value
                print (key)

需要注意的事项:

  1. 请不要使用' list'作为变量名称。
  2. 无需再次打开数据库文件。
  3. 在存储详细信息时,您将它们存储为包含(代码,价格)的元组。因此,要比较userCode,您应该将它与元组的第一个元素进行比较,而不是整个元组。
  4. 代码只是一遍又一遍地循环,因为没有退出语句退出循环。
相关问题