简体   繁体   中英

How do I make it print the outputs on same line?

I am trying to open two files and collect information for the years 2010 to 2019 and report the mean and standard deviation of just the claims in that year

mean_file = open('data/mean.txt', 'r')
std_file = open('data/std.txt', 'r')
count = 2009

for line in std_file:
    std = float(line)
    for line in mean_file:
        mean = float(line)
    count += 1
    print('Year',count, 'Mean:', mean, 'Standard Deviation:', std)

mean_file.close()
std_file.close()

I have the code above. The output I got is

Year 2019 Mean: 217557.4 Standard Deviation: 82296.33
Year 2019 Mean: 217557.4 Standard Deviation: 77808.0
Year 2019 Mean: 217557.4 Standard Deviation: 66939.77
Year 2019 Mean: 217557.4 Standard Deviation: 65486.56
Year 2019 Mean: 217557.4 Standard Deviation: 59126.12
Year 2019 Mean: 217557.4 Standard Deviation: 58712.14
Year 2019 Mean: 217557.4 Standard Deviation: 55465.54
Year 2019 Mean: 217557.4 Standard Deviation: 44621.54
Year 2019 Mean: 217557.4 Standard Deviation: 47821.1
Year 2019 Mean: 217557.4 Standard Deviation: 43170.7

Each time I change the position of the indent, it gives a different answer. I want the mean and standard deviation to be printed on the same line simultaneously just like the output below. I want the output to be like below. How do I make it print exactly like the output below?

Year 2010 Mean: 455692.98 Standard Deviation: 82296.33
Year 2011 Mean: 409110.4 Standard Deviation: 77808.0
Year 2012 Mean: 372226.67 Standard Deviation: 66939.77
Year 2013 Mean: 341826.79 Standard Deviation: 65486.56
Year 2014 Mean: 306567.67 Standard Deviation: 59126.12
Year 2015 Mean: 276956.5 Standard Deviation: 58712.14
Year 2016 Mean: 263900.21 Standard Deviation: 55465.54
Year 2017 Mean: 243116.25 Standard Deviation: 44621.54
Year 2018 Mean: 220894.98 Standard Deviation: 47821.1
Year 2019 Mean: 217557.4 Standard Deviation: 43170.7

EDIT: Apologies, made a mistake. I see where you are coming from. All the points below, other than indentation, still apply. To fix this error, you should use zip , which allows you to iterate both files at once:

with open('std.txt', 'r') as std_file:
    with open('means.txt', 'r') as mean_file:
        for line in zip(std_file,mean_file):
            std = float(line[0])
            mean = float(line[1])
            count += 1
            print('Year',count, 'Mean:', mean, 'Standard Deviation:', std)

Which gives us:

Year 2010 Mean: 455692.98 Standard Deviation: 82296.33
Year 2011 Mean: 409110.4 Standard Deviation: 77808.0
Year 2012 Mean: 372226.67 Standard Deviation: 66939.77
Year 2013 Mean: 341826.79 Standard Deviation: 65486.56
Year 2014 Mean: 306567.67 Standard Deviation: 59126.12
Year 2015 Mean: 276956.5 Standard Deviation: 58712.14
Year 2016 Mean: 263900.21 Standard Deviation: 55465.54
Year 2017 Mean: 243116.25 Standard Deviation: 44621.54
Year 2018 Mean: 220894.98 Standard Deviation: 47821.1
Year 2019 Mean: 217557.4 Standard Deviation: 43170.7

Some quick pointers that will make your code easier to read, and thus easier to debug: Use context managers (ie with statements), and don't reuse iteration variables in nested for-loops.

Using a context manager automatically opens and closes a file, and is generally safer. You can do it like so:

with open('data/std.txt', 'r') as std_file:
    with open('data/mean.txt', 'r') as mean_file:
        #do stuff

Additionally, reusing line in both for-loops is not only bad practice, but will inevitably cause the program to rewrite variables you still want. Finally, your indentation is wrong: you are only printing after iteration through ALL of the mean file, and as such will always get the same output for mean. Putting this all together, we have.

count = 2009
with open('data/std.txt', 'r') as std_file:
    with open('data/mean.txt', 'r') as mean_file:
        for line_std in std_file:
            std = float(line_std)
            for line_mean in mean_file:
                mean = float(line_mean)
                count += 1
                print('Year',count, 'Mean:', mean, 'Standard Deviation:', std)

Which should work as intended. SEE ABOVE FOR FIXED CODE

for sline, mline in zip(std_file, mean_file):
   std = float(line)
   mean = float(line)
   count = count+1
   print('Year',count, 'Mean:', mean, 'Std:', std)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM