I have a python program which takes random numbers as input and give a number of separate outputs. The run file is from This answerThe norm of vector. So how to modify the run file to get each new (x,y,z) output in seperate data files like 1.txt,2.txt....20.txt
Asked
Active
Viewed 31 times
1 Answers
1
Keeping the same 'Test.py'
import math
class Vector():
def __init__(self,vx,vy,vz):
self.x=vx
self.y=vy
self.z=vz
def norm(self):
xx=self.x**2
yy=self.y**2
zz=self.z**2
return math.sqrt(xx+yy+zz)
Add a counter to keep track of the valid output file to be created
import math
import numpy as np
from Desktop import Test
def random_range(n, min, max):
return min + np.random.random(n) * (max - min)
file_count = 1
x = random_range(20,2,9)
y = random_range(20,2,9)
z = random_range(20,2,9)
trial_args = np.stack((x, y, z), axis=-1)
for x, y, z in trial_args:
model=Test.Vector(x,y,z)
if model.norm() > 5:
ifp = open(str(file_count) + ".txt", "w")
ifp.write("{}, {}, {} => {}".format(x, y, z, model.norm()))
ifp.close()
file_count += 1

ranka47
- 995
- 8
- 25
-
If there is no if statement and I want to print all outputs as file than ifp causing problem – user105697 Feb 26 '20 at 06:45
-
1Missed a bracket in the ifp.write line. Try again. – ranka47 Feb 26 '20 at 14:57