Reputation: 556
I have a text file I am designing for a mock store I named 'Bodn's Superstore'.
I designed a .txt database as such.
iPhone
28273139
5.50
Book
81413852
1.50
Charger
62863152
3.00
Plug
25537398
4.50
You can see it follows the format
Name
Code
Price
The code I have written is intended to conver the database to a dictionary, and the customer can choose the quantity of products they buy. They then enter the 8 digit code into the computer and the IDLE works to figure out the name of the product.
The code is displayed below. It first validates the code to ensure it is 8 characters long. (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)
Now, the validation of the code works. Say for example, I want to buy 1 iPhone. This is what appears in the main window.
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 >>
And so on and so forth. The code simply won't work backwards to find the key of the dictionary and print it, which is "iPhone". I want to receive the name "iPhone" after I've stated my quantity and the product code, but my Python File won't work back through the dictionary to find the key that corresponds to the product code (value) I have given it.
Upvotes: 0
Views: 75
Reputation: 1422
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)
Couple of things to note:
Upvotes: 1
Reputation: 16224
I don't see why for line in searchfile
is required; seems like a copy mistake.
Anyway, userCode
never equals value
because value
is a tuple; it can however equal value[0]
, where you saved the codes.
How about that?
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)
Upvotes: 1