Write a function named "internet_histogram" that takes no parameters and does not return a value. There will be a file named "survey.csv" in the testing environment containing survey results as described above (the file has a header line which much be addressed by your code). Write a new file named "histogram.csv" containing 2 columns representing "internet_use,frequency" and no header line that will contain a histogram of the results of responders ages 28 to 29 including the endpoint ages. Your file will have exactly 6 lines with internet_use values of 1-5 corresponding to the intfreq results and 6 for responders who answered 2 to eminuse. Read the survey results file and track how many responders in this age range answered with each of these 6 options and write these counts to your "histogram.csv" file in order of internet_use starting with 1. Example histogram.csv: 1,5 2,7 3,0 4,1 5,2 6,4
my code:
import csv
def internet_histogram():
count_6 = 0
count_5 = 0
count_4 = 0
count_3 = 0
count_2 = 0
count_1 = 0
with open("survey.csv",'r') as f:
reader = csv.reader(f)
with open("histogram.csv", 'w') as g:
writer = csv.writer(g)
next(reader)
for line in reader:
if int(line[3]) >= 28 and int(line[3]) <= 29:
if line[2] != '':
if int(line[2]) == 1:
count_1 += 1
if int(line[2]) == 2:
count_2 += 1
if int(line[2]) == 3:
count_3 += 1
if int(line[2]) == 4:
count_4 += 1
if int(line[2]) == 5:
count_5 += 1
else:
count_6 = count_6 + 1
arr = [[1, count_1], [2, count_2], [3, count_3], [4, count_4], [5, count_5], [6, count_6]]
for i in arr:
writer.writerow(i)
output: wrote: "1,26 2,29 3,2 4,3 5,1 6,1 " expected: "1,26 2,29 3,2 4,3 5,1 6,2 "
i think its an issue with the else statement but I'm not quite sure, any help would be greatly appreciated.
The way it's written now you'll always get a count of 1
for the 6
column.
You should indent the else
so as to count this case along with the others, but using else
would only refer to the last if
, so it would count all the cases for which int(line[2]) != 5
, which probably isn't what you're trying to do.
For best python zen explicitness, I would use the following instead of else
:
if int(line[2]) not in [1, 2, 3, 4, 5]:
count_6 += 1
Does that work for you?
Good luck!
Not too well-versed in Python yet myself, but I fixed your program by shifting some indentation here, along with a few other minor changes:
import csv
def internet_histogram():
count_6 = 0
count_5 = 0
count_4 = 0
count_3 = 0
count_2 = 0
count_1 = 0
with open("survey.csv",'r') as f:
reader = csv.reader(f)
with open("histogram.csv", 'w') as g:
writer = csv.writer(g)
next(reader)
for line in reader:
if int(line[3]) >= 28 and int(line[3]) <= 29:
if line[2] != '':
if int(line[2]) == 1:
count_1 += 1
elif int(line[2]) == 2:
count_2 += 1
elif int(line[2]) == 3:
count_3 += 1
elif int(line[2]) == 4:
count_4 += 1
elif int(line[2]) == 5:
count_5 += 1
else:
count_6 = count_6 + 1
arr = [[1, count_1], [2, count_2], [3, count_3], [4, count_4], [5, count_5], [6, count_6]]
for i in arr:
writer.writerow(i)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.