Reading data from a text file, must use append - Python












0














I have to create a program that reads a text file of lengths of boards. I have to find and print the average of these lengths. Then I have to print a table of lengths and their differences from the mean. Then I need to count how many differences are > 0.10.



So far I have been able to find the average of the boards and I have been able to count the amount of differences more than > 0.10. But I cannot figure out how to append the lengths and differences into a list to be able to print each length with the difference next to it.



Code:



dif = 0
total = 0
count = 0
avg = 0
difcount = 0
boards =
with open("boards.txt", "r") as fo:
for line in fo:
length = float(line)
total += length
count += 1
avg = total/count
dif = length - float(avg)
if dif > float(0.10):
difcount += 1
print("Average:", round(avg,2))
for line in fo:
print(length, dif)
print("Number of boards > 0.10 from average:", difcount)


Output:



average: 7.97
Number of boards > 0.10 from average:3


I can't figure out how to print my table of lengths and differences



Length    Difference
# #
# #
# and so on









share|improve this question





























    0














    I have to create a program that reads a text file of lengths of boards. I have to find and print the average of these lengths. Then I have to print a table of lengths and their differences from the mean. Then I need to count how many differences are > 0.10.



    So far I have been able to find the average of the boards and I have been able to count the amount of differences more than > 0.10. But I cannot figure out how to append the lengths and differences into a list to be able to print each length with the difference next to it.



    Code:



    dif = 0
    total = 0
    count = 0
    avg = 0
    difcount = 0
    boards =
    with open("boards.txt", "r") as fo:
    for line in fo:
    length = float(line)
    total += length
    count += 1
    avg = total/count
    dif = length - float(avg)
    if dif > float(0.10):
    difcount += 1
    print("Average:", round(avg,2))
    for line in fo:
    print(length, dif)
    print("Number of boards > 0.10 from average:", difcount)


    Output:



    average: 7.97
    Number of boards > 0.10 from average:3


    I can't figure out how to print my table of lengths and differences



    Length    Difference
    # #
    # #
    # and so on









    share|improve this question



























      0












      0








      0







      I have to create a program that reads a text file of lengths of boards. I have to find and print the average of these lengths. Then I have to print a table of lengths and their differences from the mean. Then I need to count how many differences are > 0.10.



      So far I have been able to find the average of the boards and I have been able to count the amount of differences more than > 0.10. But I cannot figure out how to append the lengths and differences into a list to be able to print each length with the difference next to it.



      Code:



      dif = 0
      total = 0
      count = 0
      avg = 0
      difcount = 0
      boards =
      with open("boards.txt", "r") as fo:
      for line in fo:
      length = float(line)
      total += length
      count += 1
      avg = total/count
      dif = length - float(avg)
      if dif > float(0.10):
      difcount += 1
      print("Average:", round(avg,2))
      for line in fo:
      print(length, dif)
      print("Number of boards > 0.10 from average:", difcount)


      Output:



      average: 7.97
      Number of boards > 0.10 from average:3


      I can't figure out how to print my table of lengths and differences



      Length    Difference
      # #
      # #
      # and so on









      share|improve this question















      I have to create a program that reads a text file of lengths of boards. I have to find and print the average of these lengths. Then I have to print a table of lengths and their differences from the mean. Then I need to count how many differences are > 0.10.



      So far I have been able to find the average of the boards and I have been able to count the amount of differences more than > 0.10. But I cannot figure out how to append the lengths and differences into a list to be able to print each length with the difference next to it.



      Code:



      dif = 0
      total = 0
      count = 0
      avg = 0
      difcount = 0
      boards =
      with open("boards.txt", "r") as fo:
      for line in fo:
      length = float(line)
      total += length
      count += 1
      avg = total/count
      dif = length - float(avg)
      if dif > float(0.10):
      difcount += 1
      print("Average:", round(avg,2))
      for line in fo:
      print(length, dif)
      print("Number of boards > 0.10 from average:", difcount)


      Output:



      average: 7.97
      Number of boards > 0.10 from average:3


      I can't figure out how to print my table of lengths and differences



      Length    Difference
      # #
      # #
      # and so on






      python






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 at 0:40









      Prune

      42.5k143454




      42.5k143454










      asked Nov 16 at 0:28









      H. Raydon

      584




      584
























          3 Answers
          3






          active

          oldest

          votes


















          1














          Your computations aren't in the correct order. You can't do any differences until you know the mean of the inputs. At the moment, there's no way for the first board to get flagged, because your avg at that point is nothing more than the first board's length.



          You need to handle this in steps:




          1. Read all the data into a list

          2. Compute the average

          3. Go back through the list, checking each item against the average.


          In this third step, you should have an easy time producing your output.



          Can you take it from there?






          share|improve this answer





























            0














            I don't think your length variable is instantiated when you're trying to print it.
            You could create a tuple array to hold your lengths and difs:




            for line in fo:

            boards.append((length, dif))
            for item in boards:
            print(item)



            that way you'd have them after reading the text file.






            share|improve this answer





























              0














              Prune's answer is true, I don't think you are calculating the averages correctly.



              I think that you should first loop though all the boards and store their length in a list



              board_lengths = 
              with open("boards.txt", "r") as fo:
              for line in fo:
              board_lengths.append(float(line))


              After you have done this you could sum up the list to create you mean



              import numpy as np
              mean_board_length = np.mean(board_lengths)


              You could find then find the differences



              differences = [l - mean_board_length for l in board_lengths]


              and the amount of times when the difference is greater than 0.1



              times = np.sum([d>0.1 for d in differences])





              share|improve this answer





















                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329742%2freading-data-from-a-text-file-must-use-append-python%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1














                Your computations aren't in the correct order. You can't do any differences until you know the mean of the inputs. At the moment, there's no way for the first board to get flagged, because your avg at that point is nothing more than the first board's length.



                You need to handle this in steps:




                1. Read all the data into a list

                2. Compute the average

                3. Go back through the list, checking each item against the average.


                In this third step, you should have an easy time producing your output.



                Can you take it from there?






                share|improve this answer


























                  1














                  Your computations aren't in the correct order. You can't do any differences until you know the mean of the inputs. At the moment, there's no way for the first board to get flagged, because your avg at that point is nothing more than the first board's length.



                  You need to handle this in steps:




                  1. Read all the data into a list

                  2. Compute the average

                  3. Go back through the list, checking each item against the average.


                  In this third step, you should have an easy time producing your output.



                  Can you take it from there?






                  share|improve this answer
























                    1












                    1








                    1






                    Your computations aren't in the correct order. You can't do any differences until you know the mean of the inputs. At the moment, there's no way for the first board to get flagged, because your avg at that point is nothing more than the first board's length.



                    You need to handle this in steps:




                    1. Read all the data into a list

                    2. Compute the average

                    3. Go back through the list, checking each item against the average.


                    In this third step, you should have an easy time producing your output.



                    Can you take it from there?






                    share|improve this answer












                    Your computations aren't in the correct order. You can't do any differences until you know the mean of the inputs. At the moment, there's no way for the first board to get flagged, because your avg at that point is nothing more than the first board's length.



                    You need to handle this in steps:




                    1. Read all the data into a list

                    2. Compute the average

                    3. Go back through the list, checking each item against the average.


                    In this third step, you should have an easy time producing your output.



                    Can you take it from there?







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 16 at 0:37









                    Prune

                    42.5k143454




                    42.5k143454

























                        0














                        I don't think your length variable is instantiated when you're trying to print it.
                        You could create a tuple array to hold your lengths and difs:




                        for line in fo:

                        boards.append((length, dif))
                        for item in boards:
                        print(item)



                        that way you'd have them after reading the text file.






                        share|improve this answer


























                          0














                          I don't think your length variable is instantiated when you're trying to print it.
                          You could create a tuple array to hold your lengths and difs:




                          for line in fo:

                          boards.append((length, dif))
                          for item in boards:
                          print(item)



                          that way you'd have them after reading the text file.






                          share|improve this answer
























                            0












                            0








                            0






                            I don't think your length variable is instantiated when you're trying to print it.
                            You could create a tuple array to hold your lengths and difs:




                            for line in fo:

                            boards.append((length, dif))
                            for item in boards:
                            print(item)



                            that way you'd have them after reading the text file.






                            share|improve this answer












                            I don't think your length variable is instantiated when you're trying to print it.
                            You could create a tuple array to hold your lengths and difs:




                            for line in fo:

                            boards.append((length, dif))
                            for item in boards:
                            print(item)



                            that way you'd have them after reading the text file.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 16 at 0:38









                            tazzledazzle

                            547




                            547























                                0














                                Prune's answer is true, I don't think you are calculating the averages correctly.



                                I think that you should first loop though all the boards and store their length in a list



                                board_lengths = 
                                with open("boards.txt", "r") as fo:
                                for line in fo:
                                board_lengths.append(float(line))


                                After you have done this you could sum up the list to create you mean



                                import numpy as np
                                mean_board_length = np.mean(board_lengths)


                                You could find then find the differences



                                differences = [l - mean_board_length for l in board_lengths]


                                and the amount of times when the difference is greater than 0.1



                                times = np.sum([d>0.1 for d in differences])





                                share|improve this answer


























                                  0














                                  Prune's answer is true, I don't think you are calculating the averages correctly.



                                  I think that you should first loop though all the boards and store their length in a list



                                  board_lengths = 
                                  with open("boards.txt", "r") as fo:
                                  for line in fo:
                                  board_lengths.append(float(line))


                                  After you have done this you could sum up the list to create you mean



                                  import numpy as np
                                  mean_board_length = np.mean(board_lengths)


                                  You could find then find the differences



                                  differences = [l - mean_board_length for l in board_lengths]


                                  and the amount of times when the difference is greater than 0.1



                                  times = np.sum([d>0.1 for d in differences])





                                  share|improve this answer
























                                    0












                                    0








                                    0






                                    Prune's answer is true, I don't think you are calculating the averages correctly.



                                    I think that you should first loop though all the boards and store their length in a list



                                    board_lengths = 
                                    with open("boards.txt", "r") as fo:
                                    for line in fo:
                                    board_lengths.append(float(line))


                                    After you have done this you could sum up the list to create you mean



                                    import numpy as np
                                    mean_board_length = np.mean(board_lengths)


                                    You could find then find the differences



                                    differences = [l - mean_board_length for l in board_lengths]


                                    and the amount of times when the difference is greater than 0.1



                                    times = np.sum([d>0.1 for d in differences])





                                    share|improve this answer












                                    Prune's answer is true, I don't think you are calculating the averages correctly.



                                    I think that you should first loop though all the boards and store their length in a list



                                    board_lengths = 
                                    with open("boards.txt", "r") as fo:
                                    for line in fo:
                                    board_lengths.append(float(line))


                                    After you have done this you could sum up the list to create you mean



                                    import numpy as np
                                    mean_board_length = np.mean(board_lengths)


                                    You could find then find the differences



                                    differences = [l - mean_board_length for l in board_lengths]


                                    and the amount of times when the difference is greater than 0.1



                                    times = np.sum([d>0.1 for d in differences])






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 16 at 0:44









                                    James Fulton

                                    1765




                                    1765






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329742%2freading-data-from-a-text-file-must-use-append-python%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        How to send String Array data to Server using php in android

                                        Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                                        Is anime1.com a legal site for watching anime?