0
infilehandle = open ("receipts-10-28-13.txt", "r")

# FUNCTIONS

def stripsigns( astring ):
    """Remove dollar signs"""
    signs = "$"
    nosigns = ""
    for numbers in astring:
        if numbers not in signs:
            nosigns = nosigns + numbers
    return nosigns

def totaltake():
    """Calculate total take"""
    total = 0
    for line in infilehandle:
        values = line.split(':')
        cardnumbers = values[1]
        cardnumbers = stripsigns(cardnumbers)
        total = (total + eval(cardnumbers))
        total = round(total,2)
    return total

# more code etc

def computetax(rate, total):
    total = totaltake() - totaltip()
    taxed = total * rate
    taxed = round(taxed,2)
    return taxed

# more code etc

# VARS

total = totaltake()
tips = totaltip()
tax = computetax(rate,total)

rate = eval(input("Input the tax rate as an integer:"))

# PRINT

print("Total take: $", totaltake())
print("Total tips: $", totaltips())
print("Tax owed: $", computetax(rate,total))

I'm trying to make a file that will look at elements from a txt file and then calculate things based on the numbers in the file. The functions are all either variations of the totaltake(), which is getting numbers from the file and finding the sum, or the computetax(), which takes the numbers the other functions calculate and multiplies/divides/etc. I've tested all the functions individually and they all work, but when I try to put them together in one file, it doesn't give me the output I want, and I don't know why. It prints the value of whatever is first in the vars list, and 0 for everything else -- so for the version of code I have above, it prints

Total take: $ 6533.47
Total tips: $ 0
Tax owed: $ 0

So basically, what am I doing wrong?

Aurelie
  • 3
  • 1
  • what is the expected output, and what is the input data ? – njzk2 Nov 05 '13 at 21:20
  • `eval(cardnumbers)` ? don't you mean `int(cardnumber)`? – njzk2 Nov 05 '13 at 21:21
  • I guess your issue comes from the fact that you open your input file once at the beginning, then go through it, but never reset the read pointer, makes that it points to the end of the file. You need to reopen your file at the beginning – njzk2 Nov 05 '13 at 21:23

1 Answers1

0

See Is file object in python an iterable

File objects are iterators, meaning that once a file has been iterated to completion, you cannot iterate over the file again unless it has been reset to the beginning of the file by calling file.seek(0).

When you only call totaltake(), because infilehandle has not yet been iterated, the for loop goes through all the lines in infilehandle, which is why you get the expected result.

However, when you put computetax() together with totaltake(), totaltake() gets called by itself, and then again incomputetax(). Because infilehandle has been iterated to completion the first time totaltake() is called, the for loop is not entered the second time and the initial value for total, 0, is returned.

As the value returned by totaltake() should not change, you should modify computetax() so that you can pass total as a parameter instead of calling totaltake(). You should do the same so that it also takes tips as a parameter instead of recalculating the value.

If you cannot modify computetake(), you could also seek to the beginning of infilehandle by adding infilehandle.seek(0) to the beginning of totaltake(), to allow it to be iterated again.

Community
  • 1
  • 1