Hey guys.
I'm new to Python. I kinda blundered into a CIS major at the university I attend, and I have to say, I haven't enjoyed the Python class so far. It's so... esoteric. I'm having a TON of trouble grasping what the rest of my class view as simple concepts. I read and re-read the chapters, I attend tutoring almost every day, and still this stuff isn't clicking. I'm about halfway through the semester, and I'm really starting to panic. My scholarship (and continued pursuit of higher education) is riding on me passing this Python class. I need all the help I can get, so I'm turning to the good folks of DaniWeb for a little bit of extra-extracurricular help.
My professor has assigned a problem from our textbook for us to work out. It seems like it should be simple enough-- we have to code a program that asks for the name of a .txt file from the user (the .txt file is created by the student) that contains a few lines of text and integers. Our coded program is to read that file, do a few basic computations with the integers, and then print the output in a tabular format. The integers represent hourly rates of pay and hours worked for employees at a fictitious company. I think I've got a basic working algorithm set up, which goes as follows:

create text file
open text file in python
read file (using for loop?)
check number of hours to see if the employee worked overtime (OT)
computations
-for employee with no OT
- hourly wage * hours worked
-for employee with OT
- for the first 40 hours
- hourly wage * hours worked
- for any time after
- hourly wage * 1.5 * OT hours

print name, number hours worked and salary for each employee


Assuming that algorithm would actually work, I've also started on the code:

inName = raw_input("Enter the name of the input file: ")
inputFile = open(inName, 'r')
for line in inputFile:
    wordlist = line.split()
    for word in wordlist:
        number = int(word)
    if number <= 40:

...and then I hit a roadblock. Based on what I've read from the chapter we're currently discussing, I think two for loops would be the best way to handle this problem, but I'm completely unsure of how to proceed. The area that I've colored red is basically my "guess"-- nearly everything else is straight from the book. My tutors (who are graduate students that haven't taken any python class, but have plenty of experience with C++/Java) were as helpful as they could be, but that's not saying much. They basically told me to hit the web... so here I am. I humbly ask for ANY assistance, and I thank anyone who takes the time to help me in advance. Am I even on the right track here?

Recommended Answers

All 5 Replies

Can you tell us about the format of the text file your reading - that is to say, in what order do you expect things in? For example, does it have the name of the employee followed by the hourly wage followed by the hours worked? Or is it in some other order? Are the fields of the record separated by spaces (as seems to be the case) or something else (e.g., semicolons)? could you post a few lines from the data file so we can see what you are trying to work on?

I suspect that rather than having that inner loop, you will need five different sections inside the main file loop:

for each line of the text file:
    split the line into individual words
    read in the name
    read in the hourly rate and convert it to a float
    read in the hours worked and convert it to an int
    compute the total salary
    print the result as a line of the table

The exact order of the first three will depend on file format, but the last two would definitely be at the end. I would recommend writing the part that computes the salary as a separate function, so that you can think out the logic of it independent of the rest of the program. If you need help with that ask.

Don't forget to put the float and int conversions in try: blocks, in case something is wrong in the text file:

try:
        hours = float(wordlist[2])
    except ValueError as e:
        print "The hourly rate for", name, "is not a number."

Can you tell us about the format of the text file your reading - that is to say, in what order do you expect things in? For example, does it have the name of the employee followed by the hourly wage followed by the hours worked? Or is it in some other order? Are the fields of the record separated by spaces (as seems to be the case) or something else (e.g., semicolons)? could you post a few lines from the data file so we can see what you are trying to work on?

I suspect that rather than having that inner loop, you will need five different sections inside the main file loop:

for each line of the text file:
    split the line into individual words
    read in the name
    read in the hourly rate and convert it to a float
    read in the hours worked and convert it to an int
    compute the total salary
    print the result as a line of the table

The exact order of the first three will depend on file format, but the last two would definitely be at the end. I would recommend writing the part that computes the salary as a separate function, so that you can think out the logic of it independent of the rest of the program. If you need help with that ask.

Don't forget to put the float and int conversions in try: blocks, in case something is wrong in the text file:

try:
        hours = float(wordlist[2])
    except ValueError as e:
        print "The hourly rate for", name, "is not a number."

The try blocks are not necessary in the first place. Printing "the hourly rate is not a number" is worse than letting an exception propagate, because the exception gives you the line number where the error occurred and the functions which where called, etc.

Also, for testing purposes, put the raw_input() in a comment and hard code the filename in your code: inputFile = "myfile.txt". During the development phase of your program, you are going to run it dozens of times, and there is no point in typing the filename each time.

Otherwise, Schoil-R-LEA's algorithm is probably the way to go...

The try blocks are not necessary in the first place. Printing "the hourly rate is not a number" is worse than letting an exception propagate, because the exception gives you the line number where the error occurred and the functions which where called, etc.

Ordinarily I would agree, but in this case, the error would almost always be in the data file, not the code. Therefore, it makes sense to indicate where in the data the problem occurred, and then attempt to continue processing the rest of the data.

commented: Good point of warning about specific bad data and going on +2

Sorry-- the .txt file looks like this:

Jay 10.25 45
Miter 12.00 32
Bane 17.00 40
Funk 10.25 40

with the first item being the name, second the hourly pay, and third the hours worked. Schoil-R-LEA-- I've scoured our python book and the web, and I can't seem to find a way to read all three items separately. I feel like I could finish this cursed assignment if I just knew how to call them into the interpreter and assign them to variables, because I've coded salary-finding programs before....

Actually, you are already breaking them down into three parts with your existing code. The split() function returns a list, which can then be either accessed by index:

for line in inputFile:
    values = line.split()
    name = values[0]
    rate = float(values[1])
    hours = int(values[2])

... or assigned directly to named variables:

for line in inputFile:
    name, rateString, hoursString = line.split()
    rate = float(rateString)
    hours = int(hoursString)

Either approach should give you the values you need out of an individual line of text; the indexing approach is probably a bit more stable, as the named variables approach could cause problems if there were accidentally more or fewer than three items on a line.

If you don't mind me asking, what languages have you worked in before? You mention that you've done similar assignments in the past, and knowing what you already know could help in putting the explanations into a familiar context.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.