I have created a list and would like to read specific items in it, I only can read 1 -
import cPickle, sys, shelve
-
-
## open file that stores scores
-
-
choice = None
-
employees = []
-
while choice != "0":
-
-
print \
-
"""
-
411 TUCKSHOP
-
-
OPTIONS:
-
0 - Exit
-
1 - Employee: Add New Employee
-
2 - Employee: Display Balance
-
3 - Show All Employees and Balances
-
4 - Employee: Make deposit
-
5 - Purchase items @ Tuck Shop
-
6 - Remove all emmployees from database
-
-
"""
-
choice = raw_input("Choice: ")
-
print
-
-
#exit if choice is "0"
-
if choice == "0":
-
print "Good bye, please call again..."
-
-
#Choice 1 - Add Employees to a file
-
elif choice == "1":
-
#Create a sequence
-
empnum = int(raw_input("Enter Employee Number: " ))
-
name = raw_input("Enter Employee Name:" )
-
sname = raw_input("Enter Employee Surname:" )
-
dept = raw_input("Enter Department:" )
-
bal = float(raw_input("Employee Balance R: "))
-
entry = (empnum, name, sname, dept, bal)
-
employees.append(entry)
-
-
#create pickle file
-
pickle_file = open("emp_data.dat", "a+")
-
cPickle.dump(entry, pickle_file)
-
#close pickle file
-
pickle_file.close()
-
-
#create a shelve
-
#empshelve = shelve.open("emp_data2.da")
-
#empshelve ["emp_number"] = [emp_number]
-
-
#Ensure sync
-
#empshelve.sync()
-
#close shelve
-
#empshelve.close()
-
-
#Choice 2 - Display Balances
-
elif choice == "2":
-
-
pickle_file = open("emp_data.dat", "r")
-
emps = cPickle.load(pickle_file)
-
#Prompt user for search
-
for entry in employees:
-
empnum, name, sname, dept, bal = entry
-
print name, "\t", bal
-
empn = int(raw_input("\nChoose employee number: "))
-
-
print "\nDisplaying Balance\n"
-
if empn in emps:
-
print "Balance ==> R ", emps[4]
-
else:
-
print "Sorry"
-
pickle_file.close()
-
7 3483
You have to check the first element of the tuple against the entry number: - found = False
-
for entry in employees:
-
if empn == entry[0]:
-
print "Balance ==> R ", entry[4]
-
found = True
-
if not found:
-
print "Not a valid employee number"
Generally, a dictionary is used, with the employee number as the key pointing to a list of items http://greenteapress.com/thinkpython/html/book012.html so you can then use, if empn in employee_dict.
A couple of hints:
# 6 is easy --> employees = [] ## re-initialize as an empty list
When adding an employee, check if the number is already in the list, since you don't want the same employee number in the list twice.
Use a function to look up an employee number, since you will be doing this for every choice on the menu.
Use a list instead of a tuple so you can change it (make deposits)
Read the pickle file once, before the while loop, instead of every time through the loop, and write/dump/close it once, after the while loop. Adds will be appended to the list that will be pickled.
All your suggestions have been invaluable and I have gotten someway but when I cPickle the file I fail to read the contents back as a list -
#Choice 2 - Display Balances
-
elif choice == "2":
-
-
lists = []
-
infile = open('emp_data.dat', 'r')
-
#inlist = cPickle.load(infile)
-
while 1:
-
try:
-
lists.append(cPickle.load(infile))
-
-
except (EOFError):
-
break
-
infile.close()
-
#Prompt user for search
-
print len(lists)
-
empn = int(raw_input("\nChoose employee number: "))
-
found = False
-
for entry in lists:
-
if empn == entry[0]:
-
print "Balance ==> R ", entry[4]
-
found = True
-
if not found:
-
print "Not a valid employee number"
hi dwblas;
I'm really am loosing my mind now. my code is only able to display the first entry, from there my if statement only evaluates to false for all other entries. please correct my code:
#Choice 2 - Display Balances
elif choice == "2":
lists = []
infile = open('emp_data.dat', 'r')
lists = cPickle.load(infile)
empn = int(raw_input("\nChoose employee number: "))
found = False
for entry in lists:
if empn == entry[0]:
print "Balance ==> R ", entry[4]
found = True
if not found:
print "Not a valid employee number"
infile.close()
The pickle statement is still wrong. The link in my previous post shows an example of pickling and unpickling. If you are not going to read the post then there is little reason to post. Also, for future problems add a print statement. In this case: - for entry in lists:
-
print "testing" empn, entry[0]
-
if empn == entry[0]:
please correct my code
That's just rude.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: lokb |
last post by:
Hi,
I have a C function fread which does reading of multiple items as
num = fread(code,1,ilen-4,ifile);
which is reading 1 byte information ilen-4 times and updating to code
and returns a...
|
by: theo |
last post by:
Program flow...load file,then extract the xml text tags from the
file,then the number of
Xml tags retrieved from the file determines the number of dropdownlist
controls
instanciated in the...
|
by: JP |
last post by:
Hi,
I am trying to loop through the listbox and read the
selected items from the list, within a CLICK event on an
aspx page. The following is what I have tried. It loops
through the listbox,...
|
by: Madison Kelly |
last post by:
Hi all,
I am new to the list and I didn't want to seem rude at all so I
wanted to ask if this was okay first.
I have a program I have written in perl which uses a postgresSQL
database as the...
|
by: Esmail Bonakarian |
last post by:
Greetings all,
What is the best way to access specific records in an Excel file?
I have an Excel file, I want to randomly and repeatedly (maybe around up to
50 times) draw some rows of data...
|
by: Chris |
last post by:
Are there any other controls which could replace List & Tree controls,
because they have limitation of accepting only 32767 items.
thanks a lot in advance.
|
by: Johnymap |
last post by:
Hi everyone
I have text file which looks like these:
"index.txt" Johan 22 sebaya
"home.hml" Mpho 23 leboa
"index.frt" Tedesca 24 teba
My problem is i want to read the number on the last...
|
by: KrazyKasper |
last post by:
Access 2003 – Multi-Column List Box – Select Multiple Items
I have a multi-column (3 columns) list box that works well to select one set of records or all sets of records (based on the first field...
|
by: Lu5ck |
last post by:
Hi all,
How do you read specific div id in a external html returned from resposnetext?
Also, is it possible to count how many div tag are there in the resposnetext?
Been googling and can't...
|
by: =?ISO-8859-1?Q?Ricardo_Ar=E1oz?= |
last post by:
ajak_yahoo wrote:
Check Paul McNett's article in FoxTalk "Exploring Python from a Visual
Foxpro Perspective" and check the code in :
http://www.paulmcnett.com/vfp/09MCNESC.zip
HTH
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |