Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

My file ran perfectly last night, I made NO changes and now all of a sudden it won't print my data anymore. HELP

+11 votes
asked Jan 16, 2020 by Soha Saeed (210 points)
Please help me understand where I went wrong I'm so lost!!! Contents of my csv file are printed in columns, however, the program runs (with NO errors) and simply ends. The code is as follows:

#time import is used to add some intentional delays in the program
import time

from ReusableCustomerObject import customer

filePath = 'fit_clinic_200.csv'

#Creating an empty array to store all client data
objectArr = []

#OPENING THE FILE AND ADDING OBJECTS TO THE EMPTY ARRAY CREATED
fileHandle =  open(filePath, 'r')

#FORMAT METHOD WHICH WILL DISPLAY DIRECTORY AND EXTRA INFO (REVENUE, POP. PURCHASE AND AVG)
def formatClient(objectArr):

    for i in range(len(objectArr)):

        '''
        Each line of the csv file is turned into a separate
        customer object, this way entire objects will be sorted
        later. For each line data is split using a comma, ','
        '''
        
        vals = fileHandle.readline()
        last_name, first_name, gender, age, weight, height, purchase, cost = vals.split(',')
        person = customer(last_name, first_name, gender, age)
        objectArr.append(person)

        '''
        This code adds weight, height, purchase and cost data
        for each client to the objectArr array. This was done separetly since
        the ReusableCustomberObject file does not include these arguements in
        the constructor function
        '''
        
        objectArr[i].set_weight(weight)
        objectArr[i].set_height(height)
        objectArr[i].set_purchase(purchase)
        objectArr[i].set_cost(cost)
    
       
    for i in range (len(objectArr)):
        for j in range (len(objectArr)-1):
            if (objectArr[i].get_last_name()<objectArr[j].get_last_name()):
                    temp = objectArr[i]
                    objectArr[i] = objectArr[j]
                    objectArr [j] = temp
        '''
        A bubble sort algorithm is used to sort the client
        objects by last name. This is done by using a temp
        variable so that no object is lost in the process
        '''

    print ('%15s\t%15s\t%10s\t%10s\t%-0.6s\t%-0.6s\t%15s\t%15s' % ('First Name', 'Last Name', 'Gender', 'Age', 'Height', 'Weight' ,'Purchase', 'Cost'))

    '''
    The modulo operator is used in the print statement to provide
    a structured output. The number in after the % sign represents
    a fixed character width.
    '''
    
    for i in range (0, len(objectArr)):
        print ('%15s\t%15s\t%10s\t%10d\t%-0.1f\t%-0.1f\t%15s\t%15s' % (objectArr[i].get_first_name(),objectArr[i].get_last_name(),objectArr[i].get_gender(),objectArr[i].set_age(),objectArr[i].get_height(), objectArr[i].get_weight(),objectArr[i].get_purchase(),objectArr[i].get_cost()))
    
    '''
    The modulo operator is used in the print statement to provide
    a structured output. The number in after the % sign represents
    a fixed character width. In this particular output, since the
    data types of each value varry (string, float,integer), there is
    a letter associated with the modulo operator
    '''    

    print('')
    
    #Calculating revenue section
    sumR = 0
    #intializing the sum variable
    
    for i in range (0, len(objectArr)):
        sumR = (float(objectArr[i].get_cost().lstrip('$')))+sumR

        '''
        The value of sumR is added to in each iteration of the loop.
        Specifically, the cost method carrying the dollar amount of
        the purchase is called, then the dollar symbol is stripped
        using the built-in l strip method and the amount is added to
        the sumR variable.
        '''

    #The total revenue, sumR is printed
    print ('Total revenue : $', sumR)

    print('')

    #Calculating average purchase value
    avg = sumR/200
    
    '''
    The sum of all purchases made is already stored in the sumR
    variable from when revenue was calculated. To calculate the
    average purchase value, this value can be divided by the number
    of clients there are; 200 clients
    '''
    #The average purchase value is printed
    print('The average purchase value is $', avg)

    print ('')

    #Finding the most popular service offered
    
    #Three counters are intialized, one for massage, one for acupuncture and one for chiropractic
    countM = 0
    countA = 0
    countC = 0

    '''
    I decided to do this probelm by recording the number of
    times each purchase occurs in the client data and comparing
    the numbers to each other.
    '''

    for i in range (0, len(objectArr)):
        #This main for loop iterates through all client data to get the purchase string values
        if (objectArr[i].get_purchase() == 'Massage'):
            countM = countM + 1
            #Each time 'Massage' is read, the counter increases by 1
            
        if (objectArr[i].get_purchase() == 'Acupuncture'):
            countA = countA + 1
            #Each time 'Acupuncture' is read, the counter increases by 1
            
        if (objectArr[i].get_purchase() == 'Chiropractic'):
            countC = countC + 1
            #Each time 'Chiropractice' is read, the counter increases by 1

    #The comparison of the counter values is done here
    if ((countM > countA) and (countM > countC)):
        '''
        If the value of countM is greater than countA
        and countC, then the massage purchase is the
        most popular amongst the three and this message
        is printed
        '''
        print ('The most popular purchase has been: Massage')

    if ((countA > countM) and (countA > countC)):
        '''
        If the value of countA is greater than countM
        and countC, then the acupunture purchase is the
        most popular amongst the three and this message
        is printed
        '''
        print ('The most popular purchase has been: Acupuncture')

    if ((countC > countA) and (countC > countM)):
        '''
        If the value of countC is greater than countA
        and countM, then the chiropractic purchase is the
        most popular amongst the three and this message
        is printed
        '''
        print ('The most popular purchase has been: Chiropractic')
    
print ('Welcome to the Fit Clinic 200 Directory')
print ('Please hold while the directory loads...')

print ('')
print ('')

time.sleep(2)
formatClient(objectArr)

print('')
print ('Thank you for visiting the Fit Clinic Directory!')

2 Answers

0 votes
answered Jan 14, 2022 by Sam (1,310 points)
with the sound of ur question did you save ur code?
0 votes
answered Jan 17, 2022 by Peter Minarik (86,040 points)

When I try to run your program I've got the following error:

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    from ReusableCustomerObject import customer
ModuleNotFoundError: No module named 'ReusableCustomerObject'

Could you please link your whole project as it cannot run without the dependant module?

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...