[英]writing files python i think i have issue with else:count
Write a function named "internet_histogram" that takes no parameters and does not return a value.编写一个名为“internet_histogram”的函数,它不带参数也不返回值。 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).
测试环境中将有一个名为“survey.csv”的文件,其中包含如上所述的调查结果(该文件有一个标题行,大部分由您的代码处理)。 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.
编写一个名为“histogram.csv”的新文件,其中包含 2 列代表“internet_use,frequency”,并且没有标题行,其中将包含 28 至 29 岁响应者的结果直方图,包括端点年龄。 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.
您的文件将正好有 6 行,internet_use 值为 1-5,对应于 intfreq 结果,6 行用于回答 2 为 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
阅读调查结果文件并跟踪在此年龄范围内回答了这 6 个选项中的每个选项的响应者数量,并将这些计数按照 internet_use 从 1 开始的顺序写入您的“histogram.csv”文件。示例 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 "输出:写道:“1,26 2,29 3,2 4,3 5,1 6,1”预期:“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.我认为这是 else 语句的一个问题,但我不太确定,任何帮助将不胜感激。
The way it's written now you'll always get a count of 1
for the 6
column.按照现在的编写方式,第
6
列的计数始终为1
。
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.您应该缩进
else
以便将此案例与其他案例一起计算,但使用else
只会引用最后一个if
,因此它将计算int(line[2]) != 5
所有案例,这可能不是不是你想要做的。
For best python zen explicitness, I would use the following instead of else
:为了最好的python zen显式,我将使用以下而不是
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:我自己还不太精通 Python,但我通过在此处移动一些缩进以及其他一些小的更改来修复您的程序:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.