Ghassan Chaar
Ghassan Chaar

Reputation: 13

Currently learning about reading from a file

guys first time asking a question here. Im taking a beginner programming course in python and was wondering why my code isnt working. It is supposed to read information from a file and output the numbers to the user, add all of them up, and give an average. Everything was working till I had to make a couple changes to the program. My problem seems to be in the CalcTotal module. I added the text from the file being read at the bottom of my code. I get this message.

fltTotal += float(strRead)

ValueError: could not convert string to float: 

Code:

def main():

    strMenu = ' '
    while strMenu != 'X':

        print('*************************************************')
        print('*                 Popcorn Sales                 *')
        print('*************************************************')
        print('\n' *3)
        print('D: Display Sales')
        print('C: Calculate Totals')
        print('X: Exit Application')
        print()
        strMenu = (str(input('Enter your Menu Selection: '))).upper()

        if strMenu == 'D': 
            DisplaySales()
        elif strMenu == 'C':
            fltTotal, fltAverage = CalcTotal()
            print(' ')
            print('*************************************')
            print('Popcorn Totals                 ')
            print('*************************************')
            print(' ')
            print('Total popcorn sales: $' + str(format(fltTotal, '.2f')))
            print('Average popcorn sales: $' + str(format(fltAverage, '.2f')))
            print(' ')
            input('Press enter to continue...')



def DisplaySales():

    print(' ')
    print('*************************************')
    print('Popcorn Sales                 ')
    print('*************************************')
    print(' ')


    try:
        intListNumber = 0
        SalesFile = open('saledata.txt', 'r')   
        strRead = '1'

        intListNumber = 0

        while strRead != '':

            intListNumber += 1
            strRead = SalesFile.readline()
            if strRead != '':
                print(str(intListNumber) + ': $' + strRead)

    except IOError:
        print('An error occurred')
    print(' ')
    input('Press enter to continue...')

def CalcTotal():

    try:
        intListNumber = 0
        SalesFile = open('saledata.txt', 'r')   
        strRead = '1'
        fltTotal = 0


        while strRead != '':
            strRead = SalesFile.readline()
            intListNumber += 1
            fltTotal += float(strRead)
        fltAverage = fltTotal / intListNumber




    except IOError:
        print('An error occurred')
    return fltTotal, fltAverage

main()

data file:

50

17.32

32.99

51.02

15.61

23.94

5.99

12.1

62.74

105.59

16.50

32.99

23.71

54.90

19

17.52

48.6

102

99.99

Upvotes: 1

Views: 80

Answers (1)

prhmma
prhmma

Reputation: 953

here is a little fix for your code

while strRead != '':
    strRead = SalesFile.readline()
    if len(strRead)!=0:
        intListNumber += 1
        fltTotal += float(strRead)
fltAverage = fltTotal / intListNumber

you have to check if what you read is valid and not empty. there are many other ways like what you are doing with try,expect to validate entry and avoid such problems. since this way you actually can not read the whole file, and just the first element due to format of the file, is suggest to rewrite the function like this:

def CalcTotal():

    try:
        intListNumber = 0
        SalesFile = open('saledata.txt', 'r')   
        strRead = '1'
        fltTotal = 0

        with open('saledata.txt', 'r') as openfileobject:
            for line in openfileobject:
                try:
                    print(line)
                    fltTotal += float(line)
                    intListNumber += 1
                except: pass
        fltAverage = fltTotal / intListNumber

I used print(line) there to show you how it works.

Upvotes: 1

Related Questions