尝试从另一个文件返回带有参数的结果时遇到问题" calcFun"到" Main"。我能够输入我所有的x,y,z值,只是不能从Calc中得到任何东西" calcFun"我试过了
print calcFun.Calc(x1Coordinates, x2Coordinates, y1Coordinates, y2Coordinates,
z1Coordinates, z2Coordinates)
在main中,但它告诉我这没有定义。
主要
import calcFun
calcFun.Header()
doAnother = 'y'
while doAnother.lower() == 'y':
# functions to ask user to enter information to calulate 3 dimensional points
calcFun.GetX1()
calcFun.GetX2()
calcFun.GetY1()
calcFun.GetY2()
calcFun.GetZ1()
calcFun.GetZ2()
print calcFun.Calc(x1Coordinates, x2Coordinates, y1Coordinates, y2Coordinates,
z1Coordinates, z2Coordinates)
doAnother = raw_input('\nCalulate another? (y/n) ')
print ('Good Bye!')
calcFun
import math
# -----Functions-----
# Header
def Header():
print "Header"
# X1 Coordinates
def GetX1():
x1Coordinates = input('Enter x1 Coordinate: Example. 2 ')
return x1Coordinates
# X2 Coordinates
def GetX2():
x2Coordinates = input('Enter x2 Coordinate: Example. 5 ')
return x2Coordinates
# Y1 Coordinates
def GetY1():
y1Coordinates = input('Enter y1 Coordinates: Example. 5 ')
return y1Coordinates
# Y2 Coordinates
def GetY2():
y2Coordinates = input('Enter y2 Coordinates: Example. 4 ')
return y2Coordinates
# Z1 Coordinates
def GetZ1():
z1Coordinates = input('Enter z1 Coordinates: Example. 7 ')
return z1Coordinates
# Z2 Coordinates
def GetZ2():
z2Coordinates = input('Enter z2 Coordinates: Example. 9 ')
return z2Coordinates
# Calc Function
# Calc will take x1Coordinates, x2Coordinates, y1Coordinates,
# y2Coordinates, z1Coordinates and z2Coordinates as parameters.
def Calc(x1Coordinates, x2Coordinates, y1Coordinates, y2Coordinates,
z1Coordinates, z2Coordinates):
# Step 1: x1, x2, y1, y2, z1, z2 to the 2nd power & subtracting them
x = (x2Coordinates - x1Coordinates)**2
y = (y2Coordinates - y1Coordinates)**2
z = (z2Coordinates - z1Coordinates)**2
# Step 2: Adding x, y, z
xyzTotal = x + y + z
# Step 3: Square Rooting xyzTotal
answer = math.sqrt(xyzTotal)
# Calc returns summary of calulations
resultStr = '\nx1 Coordinates: ' + str(x1Coordinates)
resultStr += '\nx2 Coordinates: ' + str(x2Coordinates)
resultStr += '\n\ny1 Coordinates: ' + str(y1Coordinates)
resultStr += '\ny2 Coordinates: ' + str(y2Coordinates)
resultStr += '\n\nz1 Coordinates: ' + str(z1Coordinates)
resultStr += '\nz2 Coordinates: ' + str(z2Coordinates)
resultStr += '\nAnswer: '+ format(answer,'.2f')
return resultStr
答案 0 :(得分:2)
您没有包含有关Main.py或回溯消息的大量信息,所以我在这里猜测一下。以下是您的问题的简化版本,据我所知:
主
$ cat Main.py
import calcFun
print calcFun.Calc(10, 20)
calcFun
$ cat calcFun.py
def Calc(a, b):
resultStr = '\na' + str(a)
resultStr += '\nb' + str(b)
return resultStr
请注意,运行正常:
$ python Main.py
a10
b20
但如果我尝试使用变量名'a'和'b'
$ cat Main.py
import calcFun
print calcFun.Calc(a, b)
这会产生一个像你提到的错误,但它与你的其他函数或文件无关,你只是指不存在的变量(你需要为它们赋值):
$ python Main.py
Traceback (most recent call last):
File "Main.py", line 3, in <module>
print calcFun.Calc(a, b)
NameError: name 'a' is not defined
这与
的行为相同$ python -c 'sum([a, b])'
与
相比$ python -c 'sum([1, 2])'
顺便说一下,通常的python命名约定会让你的文件名为'main.py'和'calc_fun.py'。请参阅https://www.python.org/dev/peps/pep-0008/#package-and-module-names处的PEP8的相关部分。
答案 1 :(得分:1)
只需改变:
calcFun.GetX1()
calcFun.GetX2()
calcFun.GetY1()
calcFun.GetY2()
calcFun.GetZ1()
calcFun.GetZ2()
print calcFun.Calc(x1Coordinates, x2Coordinates, y1Coordinates, y2Coordinates,
z1Coordinates, z2Coordinates)
为:
_x1 = calcFun.GetX1()
_x2 = calcFun.GetX2()
_y1 = calcFun.GetY1()
_y2 = calcFun.GetY2()
_z1 = calcFun.GetZ1()
_z2 = calcFun.GetZ2()
print calcFun.Calc(_x1, _x2, _y1, _y2, _z1, _z2)
然后我建议仔细阅读python代码的解释方式; p 简而言之,您正在调用函数来从用户那里获取变量,但这些值并不是随处可见的。上面的更改会将值保存到变量(_x1,_x2,...)中,然后传递给下面的函数调用。