[Tutor] Need help to print outputs on seperate files and avoid some unwanted error messages which can stop my code at some mid point

SATYABRATA DATTA iamsatyabrata428 at gmail.com
Thu Feb 27 15:53:57 EST 2020


There is some code
*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)*

Now in the run file
*import math*
*import numpy as np*

*from Desktop import Test*
*def random_range(n, min, max):*
*return min + np.random.random(n) * (max - min)*
*filenumber = 0*
*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:*
*    filenumber += 1*
*    filename = str(filenumber)*
*    with open(filename+'.txt', 'w') as f:*
*        print(x, y, z, '=>', model.norm(), *

Now let say I have a similar program which need two operator ouptput means
model.operation1 #that produces some output which is used by operation2
model.operation2
Not I want to print
print(x,y,z,’=>’,model.operation1,\n model.operation2,file=(my specified
folder)f
How to write that(In case of my original program the model.operation1 and
model.operation2 when given print command gives ouput in python shell). But
since there happens to be some error at some specific points(x,y,z) which
causes my program to stop at some intermediate point. So my trick is to
print each output to seperate files wherether errorful or right and than I
can manually search which outputs are ok for me


More information about the Tutor mailing list