对函数返回感到困惑

时间:2012-12-28 02:03:42

标签: python

好的,我的第一个程序遇到了麻烦。如果你看看这两个函数,我想要的是如果“cur”值不在min和max之间,它将Target变量更改为“0”,这样我就可以在程序中使用它了。但它似乎没有这样做......我使用“返回”错了吗?基本上,函数需要将target1转换为0,以便ri_fit更改为0,以便我的最终if触发器...

对于可怕的代码感到抱歉,我才刚刚开始!

#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(">"))

4 个答案:

答案 0 :(得分:2)

当您在其中一个函数中分配目标时,您将在那里替换名为target的参数的值,而不是触及全局参数。因此,如果您希望将函数分配给全局目标,则无法在函数中定义新目标。

但是如果你不想改变你的功能,你可以使用return返回一个值的事实(你似乎理解,因为你打印它)。如果不是打印它,而是将其分配给目标,那么您将获得所需的值,无论计算出的值最终指向何处,您的函数都将起作用。

答案 1 :(得分:1)

return从函数返回一个值。您可以将结果存储在变量中。

在您的代码中,您尝试将target作为参数传递,然后设置其值,但由于整数是不可变的,因此您无法在函数中更改target

在您的情况下,我会将您的代码更改为以下内容:

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

更好的是,使用布尔值:

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

答案 2 :(得分:1)

我认为问题在于您将target作为值传递,而不是作为参考传递。在您的函数中,您应该删除target参数,而是写:

def ex_range(min, max, cur, valve):
    global target
    # ...
    # rest of your code

这样,解释器就会识别出您正在尝试设置全局target变量,而不是本地实例。

否则,您可以执行以下操作:

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

请参阅this相关问题。

答案 3 :(得分:1)

首先,我猜您在()之后需要close

target.close()

第二: in_rangeex_range完全相同!

第三,无论如何都可以,所以运行正确:

in_range(1,10,7,'Right Intake',1)
in_range(1,10,17,'Right Intake',1)

输出:

>>> 'Right Intake' is in range.
>>> 'Right Intake' is OUT OF RANGE.

因此,您传递给函数的参数可能是不正常!检查您是否正确地从文件中读取它们。

最后,避免使用minmax作为参数,因为它们是built-in Python函数。