[Tutor] Using import
Hello, I'm starting with Python and I'm trying to work with "import" but I'm having a problem. I have the file c.py (that works when executed) with a function performing a multiplication: def mult(a,x): resul=a*xreturn(resul)print 'test print'ent1=2ent3=3dedo=mult(ent1,ent3)print 'result: ',dedo and I have the file b.py that calls c.py but should only print a sentence, without use the function in c.py (I tried to use the code without the comment part but got the problem, my goal is to use b.py with the commented part being part of the code): import c ent1=2ent2=7print 'testing b.py without any operation' #saida=c.mult(ent1,ent2)#print 'the output is:',saida My problem is when I execute b.py, it executes c.py before b.py: $ python b.py test printresult: 6testing b.py without any operation How should I work with this? What I want is use the function defined in c.py inside b.py Regards,Felipe___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Read a matrix with lines in different behavior
Hello, I want to read the below matrix, identify when the characters in front of "want = " are equal to "1" and then save in an array and in an output file the characters above. But I don't know how to identify the second line and store in a variable: alpha=0 beta=2 gamma=50 want = 0 alpha=0 beta=2 gamma=50 want = 1 alpha=0 beta=2 gamma=50 want = 0 alpha=0 beta=2 gamma=50 want = 1 alpha=0 beta=2 gamma=50 want = 0 This is part of the code: try: datadir = '/home/me/Test_python/' fileHandle = open( "%s/teste.txt"%datadir, 'r' ) vector = [ ] for line in fileHandle.readlines(): line=line.strip() a = line.split(" ")[0] print a b = line.split(" ")[1] print b c = line.split(" ")[2] print c d = line.split(" ")[3] print d if d == "1": vector.append([a,b,c]) n = n + 1 fileHandle.close() file = open("saida.txt","w") for cont in range(n): file.write = vector except: print "Exception" sys.exit( 1 ) When I execute the code there is an error when the loop finds the second line [felipe@grumari Test_python]$ python verificar.py alpha=0 beta=2 gamma=50 Exception Thanks, Felipe ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor