2012-12-27 168 views
0

我正在試着制定計算氣門間隙的程序。這是我所做過的第一個節目,所以我混淆了我的方式。現在,這是迄今爲止我所擁有的。如果我輸入超出範圍的許可,它會打印出「正確的攝入量超出範圍」,就像它應該的那樣。但是,如果它在範圍內,那麼它會一次又一次地填充我的屏幕,並顯示「正確的攝入量在範圍內」,直到我殺死它爲止。我錯過了什麼?一次又一次地重複輸出

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

回答

2

你告訴它來保持循環和打印,而它在範圍:

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

我懷疑你想要一個if聲明:

if min <= cur <= max: 
    print "%r is in range." % valve 
+0

啊哈!你知道嗎,我已經把第二個函數改爲IF了,但是還沒有意識到修正了它!非常感謝,這讓我瘋狂! – Demonic

+1

@Demonic - 在那裏,做了更多的時間比我無意承認。樂於助人。 – Blair

1

必須更改值在這種情況下,它可能會停下來。

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

這意味着,你必須改變價值觀mincur之一,或max,或超過其中之一,爲了打破無限循環。

+0

正如其他人所說,我使用「而」而不是「如果」。哎呀。 :) – Demonic

0

這條線:

while min <= cur <= max: 

是你的問題,我相信。雖然會反覆評估身體,直到病情失敗。但是你的身體什麼都不會導致這種情況改變,所以它會永遠持續下去。我猜你的意思是/需要使用if而不是while那裏...

+0

是的,在「Learn Python the Hard Way」中,我還沒有'如果'。 :) – Demonic