CSE-8A / PA3 / PA 3 FINAL.py
PA 3 FINAL.py
Raw
import csv
def ingest_data(filename, fieldname):
    file_object = open(filename, newline='')
    rows = csv.reader(file_object, delimiter=',')
    headers = next(rows)
    try:
        field_idx = headers.index(fieldname)
    except ValueError:
        print('The field name', fieldname, 'does not exist in the headers.')
        print('Here are the value field names in this file:')
        for h in headers:
            print(h)
        return
    data_list = []
    count = 0
    limit = 2000 # CHANGE LIMIT
    for line in rows:
        if (count >= limit):
            print('Too many entries, returning first', limit, 'entries.')
            return data_list
        try:
            field_value = line[field_idx]
        except IndexError:
            print('Skipping row #', count, 'because field does not exist')
            continue
        data_list.append(field_value)
        count = count + 1
    return data_list
#Ingest data code ends here
Gender = ingest_data("data.csv", "Sex")
Location = ingest_data("data.csv", "Location")

num_hospital = Location.count("Hospital")
num_residence = Location.count("Residence")
num_other = Location.count("Other")

num_males = Gender.count('Male')
num_females = Gender.count('Female')
i= 0


#This defines the function analyze data
def analyze_data(g, l):


    Total_num_males_hospital = 0
    Total_num_males_residence = 0
    Total_num_males_other = 0
    Total_num_females_hospital = 0
    Total_num_females_residence = 0
    Total_num_females_other = 0

#This loop goes over the lists and would total up the amount after the limit of 2000
    for i in range(2000):
        if Gender[i] == ('Male') and Location[i] == ('Hospital'):
            Total_num_males_hospital += 1
        
        elif Gender[i] ==('Male') and Location[i] == ('Residence'):
            Total_num_males_residence += 1
        
        elif Gender[i] == ('Male') and Location[i] == ('Other'):
            Total_num_males_other += 1
            
        
        elif Gender[i] == ('Female') and Location[i] == ('Hospital'):
            Total_num_females_hospital += 1

        
        elif Gender[i] ==('Female') and Location[i] == ('Residence'):
            Total_num_females_residence += 1

        
        elif Gender[i] == ('Female') and Location[i] == ('Other'):
            Total_num_females_other += 1
   
             

            
#This returns the value from the loops


    if g == "Male" and l == "Hospital" :
        return(print("This is the number of Males that went to the Hospital because of usage of drugs:", Total_num_males_hospital))
    elif g =="Male" and l == "Residence" :
        return(print("This is the number of Males that stayed at home because of usage of drugs:", Total_num_males_residence))
    elif g =="Male" and l == "Other" :
        return(print("This is the number of Males that went to another place because of usage of drugs:", Total_num_males_other))
    elif g =="Female" and l == "Residence" :
        return(print("This is the number of Females that stayed at home because of usage of drugs:", Total_num_females_residence))
    elif g =="Female" and l == "Other" :
        return(print("This is the number of Females that went to another place because of usage of drugs:", Total_num_females_other))
    elif g =="Female" and l == "Hospital" :
        return(print("This is the number of Females that went to the hospital because of usage of drugs:", Total_num_females_hospital))
    else:
        return("Improper Input, try again")


print("Gender: 'Male' or 'Female'")
print("Location: 'Hospital', 'Residence', or 'Other'")
print("Please input a Gender and a Location to analyze_data")