[英]Confused about return in Functions
Ok, I'm having trouble with my very first program. 好的,我在第一个程序上遇到了麻烦。 If you look down to the two functions, what I want is for if the "cur" value is NOT between the min and max, for it to change the Target variable to "0" so I can use it latter in the program... But it doesn't seem to do it... Am I using "return" wrong?
如果您不看这两个函数,那么我想要的是如果“ cur”值不在最小值和最大值之间,因为它将“目标”变量更改为“ 0”,因此我可以在程序中使用它。 。但这似乎不可行...我使用“ return”错误吗? Basically, the functions need to turn
target
from 1
to 0
, so that ri_fit
changes to 0, so that my final if
thingys trigger... 基本上,函数需要把
target
从1
到0
,这样ri_fit
变为0,让我最后if
thingys触发...
Sorry for the terrible code, I've only just begun! 对不起,代码不好,我才刚刚开始!
#Clearance calculator
#clearances are in clearances.txt
targets = open("clearances.txt", "r")
lines = targets.readlines() #get target clearances from file
in_min_target = float(lines[2]) #minimum intake clearance
in_max_target = float(lines[4]) #maximum intake clearance
ex_min_target = float(lines[8]) #miminum exhaust clearances
ex_max_target = float(lines[10]) #maximum exhaust clearances
targets.close
target_intake = (in_min_target + in_max_target) / 2 #find the ideal intake
target_exhaust = (ex_min_target + ex_max_target) / 2 #find the ideal exhaust
print "Intake Min: ", in_min_target
print "Intake Max: ", in_max_target
print "Exhaust Min: ", ex_min_target
print "Exhaust Max: ", ex_max_target
print """Target intake: %r
Target Exhaust: %r""" % (target_intake, target_exhaust)
print""
print "Enter current RIGHT side Intake clearance"
cur_r_in = float(raw_input(">"))
print ""
print "Enter current RIGHT side Exhaust clearance"
cur_r_ex = float(raw_input(">"))
print ""
print "Enter current LEFT side Intake clearance"
cur_l_in = float(raw_input(">"))
print ""
print "Enter current LEFT side Exhaust clearance"
cur_l_ex = float(raw_input(">"))
target = 1
def in_range(min, max, cur, valve, target): #figures if intake valves are correct
if min <= cur <= max:
print "%r is in range." % valve
target=1
else:
print "%r is OUT OF RANGE." %valve
target=0
return target
def ex_range(min, max, cur, valve, target): #figures if exhaust valves are correct
if min <= cur <= max:
print "%r is in range." % valve
target=1
else:
print "%r is OUT OF RANGE." %valve
target=0
return target
ri_fit = 1
re_fit = 1 #Assumes all valves are right, until further notice...
li_fit = 1
le_fit = 1
valve = "Right Intake"
print in_range(in_min_target, in_max_target, cur_r_in, valve, target)
print target
if target == 0:
ri_fit = 0
print ""
valve = "Right Exhaust"
print ex_range(ex_min_target, ex_max_target, cur_r_ex, valve, target)
print ""
valve = "Left Intake"
print in_range(in_min_target, in_max_target, cur_l_in, valve, target)
print ""
valve = "Left Exhaust"
print ex_range(ex_min_target, ex_max_target, cur_l_ex, valve, target)
print ri_fit
if ri_fit==0:
print "Right intake is out of range."
print "Enter current right intake shim size."
ri_cur_shim = int(raw_input(">"))
if re_fit==0:
print "Right exhaust is out of range."
print "Enter current right exhaust shim size."
re_cur_shim = int(raw_input(">"))
if li_fit==0:
print "Right intake is out of range."
print "Enter current right intake shim size."
li_cur_shim = int(raw_input(">"))
if le_fit==0:
print "Right exhaust is out of range."
print "Enter current right exhaust shim size."
le_cur_shim = int(raw_input(">"))
When you assign to target inside one of your functions, you are replacing the value of the parameter named target in there, and not touching the global one. 在其中一个函数中分配给target时,您将在其中替换名为target的参数的值,而不接触全局函数。 So if you want your functions to assign to the global target, you can't define a new target in the function.
因此,如果要将函数分配给全局目标,则无法在函数中定义新目标。
But if you don't want to change your functions, you can use the fact that return returns a value (which you seem to understand, since you print it). 但是,如果您不想更改函数,则可以使用return返回一个值的事实(由于打印了它,您似乎已经明白了)。 If instead of printing it, you assigned it to target, then you'd get the value you want where you want it, and your function will work no matter where the value computed is ultimately destined to go.
如果不是将其打印出来,而是将其分配给目标,那么您将在所需的位置获得所需的值,并且无论最终将计算的值放在何处,函数都将起作用。
return
returns a value from a function. return
从函数返回一个值。 You can store the result in a variable. 您可以将结果存储在变量中。
In your code, you're trying to pass target
as a parameter and then set its value, but since integers are immutable, you won't be able to change target
from within your function. 在您的代码中,您尝试将
target
作为参数传递,然后设置其值,但是由于整数是不可变的,因此您将无法在函数内更改target
。
In your case, I'd change your code to something like this: 就您而言,我会将您的代码更改为以下形式:
def ex_range(min, max, cur, valve): # You don't need `target` here
if min <= cur <= max:
print "%r is in range." % valve
target=1
else:
print "%r is OUT OF RANGE." %valve
target=0
return target
target = ex_range(...) # Store the results in a variable
Better yet, use a boolean: 更好的是,使用布尔值:
def ex_range(min, max, cur, valve): # You don't need `target` here
if min <= cur <= max:
print "%r is in range." % valve
return True
else:
print "%r is OUT OF RANGE." %valve
return False
I believe the issue lies in that you are passing target
as a value, not as a reference. 我认为问题在于您将
target
作为一种价值而不是作为参考传递。 Within your functions, you should drop the target
parameter, and instead write: 在函数中,应删除
target
参数,而应编写:
def ex_range(min, max, cur, valve):
global target
# ...
# rest of your code
This way, the interpreter identifies that you're trying to set the global target
variable, and not a local instance. 这样,解释器会识别出您正在尝试设置全局
target
变量,而不是本地实例。
Otherwise, you could do something like: 否则,您可以执行以下操作:
def in_range(min, max, cur, valve): #figures if intake valves are correct
if min <= cur <= max:
print "%r is in range." % valve
return 1
else:
print "%r is OUT OF RANGE." %valve
return 0
target = in_range(...) # fill in args
print target
First, I guess you need ()
after close
: 首先,我估计
close
后需要()
:
target.close()
Second: in_range
and ex_range
are exactly the same! 第二:
in_range
和ex_range
完全相同!
Third, anyway it is OK and so runs correctly: 第三,无论如何都可以,因此可以正常运行:
in_range(1,10,7,'Right Intake',1)
in_range(1,10,17,'Right Intake',1)
output: 输出:
>>> 'Right Intake' is in range.
>>> 'Right Intake' is OUT OF RANGE.
So, it may be the arguments you are passing to functions are not OK ! 因此,可能是您传递给函数的参数不正确 ! Check if you are reading them correctly from file.
检查您是否从文件中正确读取了它们。
Finally, avoid using min
, max
as arguments as they are built-in
Python functions. 最后,避免将
min
, max
用作参数,因为它们是built-in
Python函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.