-1

我正在尝试制作一个计算气门间隙的程序。这是我制作的第一个程序,所以我一直在摸索。现在,这就是我到目前为止所得到的。如果我输入一个超出范围的许可,它会打印“Right Intake is OUT OF RANGE”,就像它应该的那样。但是如果它在范围内,那么它会一遍又一遍地用“正确的进气口在范围内”填充我的屏幕,直到我杀死它。我错过了什么?

#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=5

def in_range(min, max, cur, valve, target):
    while 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):
if min <= cur <= max:
    print "%r is in range." % valve
    target=1
else:
    print "%r is OUT OF RANGE." %valve
    target=0


valve = "Right Intake"
print in_range(in_min_target, in_max_target, cur_r_in, valve, target)

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)
4

3 回答 3

2

你告诉它在范围内时继续循环和打印:

while min <= cur <= max:
    print "%r is in range." % valve

我怀疑你想要一个if声明:

if min <= cur <= max:
    print "%r is in range." % valve
于 2012-12-27T23:48:07.877 回答
1

您必须更改条件中的值,以便它可以停止。

def in_range(min, max, cur, valve, target):
    while min <= cur <= max:
        print "%r is in range." % valve

这意味着,您必须更改其中一个值mincurmax,或多个值,以打破无限循环。

于 2012-12-27T23:48:20.393 回答
0

这一行:

while min <= cur <= max:

是你的问题,我相信。While 将重复评估附加到它的主体,直到条件失败。但是你的身体没有做任何事情来改变这种情况,所以它只会永远持续下去。我猜你的意思是/需要使用if而不是while那里......

于 2012-12-27T23:50:18.690 回答