Python files

0

I have the following code:

x=[]
x=0.1

for i in range (101):
    x = 2.6868*x - 0.2462*x**3

I want to save my 'x' data to manipulate it in other software, I did the following:

with open( 'saida.txt', 'w' ) as arq:
    for n in x:
        arq.write( str(n) + '\n' )

I do not know if it is correct, and I also do not know in which folder this 'saida.txt' was saved, how do I solve this problem

    
asked by anonymous 27.10.2017 / 18:25

1 answer

1

From what I understood from your code, you wanted to create a list to save the results of a 3rd degree function, right?

To do this, you would have to use the append method of the List object. But, first of all, we would have to change the code a bit:

x=[]
for i in range (101):
    x.append(2.6868*i - 0.2462*i**3)

arq = open('saida.txt','a')
for n in x:
    arq.write(str(n)+'\n')
arq.close()

By running this code, you would get the following result if you printed the list x:

[0.0, 2.4406, 3.404, 1.4129999999999994, -5.009600000000001, -17.341, -37.058400000000006, -65.63900000000001, -104.56, -155.29860000000002, -219.332, -298.1374, -393.192, -505.97299999999996, -637.9576000000001, -790.6229999999999, -965.4464, -1163.905, -1387.476, -1637.6366, -1915.8639999999998, -2223.6354, -2562.428, -2933.719, -3338.9856, -3779.705, -4257.3544, -4773.411, -5329.352, -5926.6546, -6566.795999999999, -7251.2534000000005, -7981.504, -8759.025, -9585.2936, -10461.787, -11389.9824, -12371.357, -13407.387999999999, -14499.552599999999, -15649.328, -16858.1914, -18127.62, -19459.091, -20854.0816, -22314.069, -23840.530400000003, -25434.943, -27098.784, -28833.5306, -30640.66, -32521.649400000002, -34477.975999999995, -36511.117, -38622.5496, -40813.751000000004, -43086.1984, -45441.369000000006, -47880.74, -50405.7886, -53017.992, -55718.8274, -58509.772000000004, -61392.303, -64367.897600000004, -67438.033, -70604.18639999999, -73867.835, -77230.456, -80693.5266, -84258.524, -87926.92540000001, -91700.208, -95579.849, -99567.3256, -103664.115, -107871.6944, -112191.541, -116625.132, -121173.9446, -125839.45599999999, -130623.1434, -135526.484, -140550.955, -145698.0336, -150969.19700000001, -156365.9224, -161889.687, -167541.968, -173324.2426, -179237.98799999998, -185284.6814, -191465.80000000002, -197782.821, -204237.22160000002, -210830.479, -217564.0704, -224439.473, -231458.164, -238621.6206, -245931.32]

The file "output.txt" would be in the folder you are manipulating in your script.  For more infos of the open function, follow the link . Also, which python function are you using?

******* Refactoring: Adding a list y to store the result of a function of x that starts with the value 0.1 and goes through a loop 100 times **

x = 0.1 y=[] for i in range(101): x = 2.6868*x - 0.2462*x**3 print(x) y.append(x)

    
28.10.2017 / 03:54