0

Below, I have a code that will use an inner loop and outer loop to create text file. I am wanting to have weight to increase by increments of 0.1 between the interval of 0.1 to 1.0. I am also wanting length to increase from 0, 40, 80, 160, 250, and 300. But the length will not increase to the next term until weight has reached 1.0 for the previous length.

The code below does not work. The txt files should look like weight_0.1_0_.txt and in it should be

  weight of box 0.1
  length of box 0

this should continue till the file weight_1.0_0_.txt and inside should be

  weight of box 1.0
  length of box 0

Once it reaches this value the next one should be weight_0.1_40_.txt and inside should be

  weight of box 0.1
  length of box 40

this should continue until all the lengths to 300 are satisfied. If anyone could help me with this code, I would deeply appreciate it.

  lengths = [0,20,40,80,160,250,300]

  for l in lengths:
      Weight = 0.1
      for i in range (10):
          input = 'weight_' +str(Weight)+ '_length_' +str(lengths)+ '_' 
          file = open(input + '.txt','w')

          file.write('weight of box ' +str(Weight)+ '\n')
          file.write('length of box ' +str(lengths)+ '\n')
          file.close()

          Weight +=0.1
      length = 1


  With this code above, the resulting file would be for example 
  weight_0.1_[0,20,40,80,160,250,300]_.txt which is not correct and inside 
  the files would be 
        weight of box 0.1
        length of box [0,20,40,80,160,250,300]
SeanL
  • 19
  • 7
  • You use `str(lengths)` where it should be `str(l)`. `lengths` is the list and the element is `l`... – Serge Ballesta Sep 26 '19 at 08:22
  • Thank you this worked. One other question, I get a few rounding errors on the values where some of the values come out to be 0.9999999999. Could this be fixed ? – SeanL Sep 26 '19 at 08:29
  • The floating point (in)accuracy is one of the highest FAQ here : [Is floating point math broken?](https://stackoverflow.com/q/588004/3545273) – Serge Ballesta Sep 26 '19 at 08:33

1 Answers1

0

You're using str(lengths) instead of str(l) in two places:

  • creation of a filename
  • writing of file's contents

str(lengths) evaluates to "[0,20,40,80,160,250,300]" - string representation of lengths array. str(l) evaluates to the correct length (string representation of an actual array element your iteration cycle is working with).

If I understood your intent correctly, the adjusted code should look something like this:

lengths = [0,20,40,80,160,250,300]

for l in lengths:
    Weight = 0.1
    for i in range (10):
        input = 'weight_' +str(Weight)+ '_length_' +str(l)+ '_' 
        file = open(input + '.txt','w')

        file.write('weight of box ' +str(Weight)+ '\n')
        file.write('length of box ' +str(l)+ '\n')
        file.close()

        Weight +=0.1
    length = 1
cegas
  • 2,823
  • 3
  • 16
  • 16
  • this worked perfectly, but some of the values have rounding errors like 0.3000000004 and 0.999999999. could this be fixed in the code above? – SeanL Sep 26 '19 at 08:30
  • Glad it worked! Can't really reproduce rounding issue, but something like Decimal (https://docs.python.org/3/library/decimal.html) could help if exact precision is required. Then you'd need to write something like `Weight = Decimal("0.1") ` and for addition use `Weigth += Decimal("0.1")`. – cegas Sep 26 '19 at 08:36
  • instead of the variable weight, use ```i / 10``` – Loïc Sep 26 '19 at 08:47