2014-10-06 47 views
0

我編寫了這段代碼,它應該比較一組行值和名義值。用戶應該輸入一個百分比值,將比較lineValue與名義值。如果lineValue在給定的名義值的百分比範圍內,它將通過爲真。我的程序並不正確地比較python中的值

我的程序只有在lineValue的數字恰好是標稱值時纔會返回true。所有其他值都是失敗的,即使它在用戶輸入的百分比範圍內。有沒有人在我的代碼中看到一個錯誤,以防止數字被註冊爲真?

nominalValue=470 
print "Nominal Resistor Value: " , nominalValue 
lineValue = [470, 358, 324, 234, 687,460] 


user_Input=raw_input("Please Enter a Tolerance %: ") 
if user_Input.isdigit(): 
    tolerance = int(user_Input) 
    if tolerance <=20 and tolerance >=1: 
     print "Tolerance Level:", user_Input 
     percentageHigh = (tolerance/100.0 + 1.00) 
     percentageLow = (1.00 - tolerance/100.0) 
     print percentageHigh 
     print percentageLow 
     highNominal = nominalValue*percentageHigh 
     lowNominal = nominalValue*percentageLow 
     print highNominal 
     print lowNominal 
     for seriesInput in lineValue: 
      if (percentageHigh*seriesInput) <= highNominal and (percentageLow*seriesInput) >= lowNominal:   
       print seriesInput,"Pass" 
       print percentageHigh*seriesInput 

      else: 
       print seriesInput,"Fail" 
       print percentageLow*seriesInput 
    else: 
     print "Please enter a value between 1-20" 
else: 
    print "Please enter a number for a percent value" 
+1

您可以包括在你的問題中運行一個程序的例子? – 2014-10-06 21:27:43

+0

Python有一個很好的比較語法:'1 <=容差<= 20'意味着與您爲檢查容差數而編寫的內容相同。 – 2014-10-06 21:34:56

+0

呃,你是通過percentHigh(和Low)乘以比較的兩邊 - 你不需要這麼做... – 2014-10-06 21:35:54

回答

2

你已經計算highNominal和lowNominal,所以你要這一行:

if seriesInput <= highNominal and seriesInput >= lowNominal:   

或@GregHewgill指出:

if lowNominal <= seriesInput <= highNominal: 
+1

再一次,'lowNominal <= seriesInput <= highNominal'意味着同樣的事情,可能更容易讀書。 – 2014-10-06 21:37:54

+0

@GregHewgill,更容易閱讀,但我總是覺得第一次輸入錯誤。 – 2014-10-06 21:39:21

1

您當前的代碼問:

if (percentageHigh*seriesInput) <= highNominal and 
    (percentageLow*seriesInput) >= lowNominal: 

但是

highNominal = nominalValue*percentageHigh 
lowNominal = nominalValue*percentageLow 

所以你的比較是等效於:

if (percentageHigh*seriesInput) <= nominalValue*percentageHigh and 
    (percentageLow*seriesInput) >= nominalValue*percentageLow: 

簡化了下來:

if seriesInput <= nominalValue and 
    seriesInput => nominalValue: 

如應該從看到它的方式明確,那隻能是真當seriesInput == nominalValue ,因爲seriesInput不能是大於和小於nominalValue

+1

不是這樣。 OP正在比較'highNominal'和'lowNominal'而不是'nominalValue'。 – 2014-10-06 21:41:08

+0

'highNominal'是'nominalValue * percentageHigh'。比較是'(percentageHigh * seriesInput)<= highNominal',相當於'(percentageHigh * seriesInput)<=(nominalValue * percentageHigh)',當你用'percentHigh'除以兩邊時,給你我所說的。 – 2014-10-06 22:13:27

1

在你的支票

if (percentageHigh*seriesInput) <= highNominal and (percentageLow*seriesInput) >= lowNominal: 

要檢查是否seriesInput的範圍範圍圍繞標稱值,顧名思義它永遠不會成爲內。你想簡單檢查seriesInput的範圍圍繞nominalValue內,像這樣

if seriesInput <= highNominal and seriesInput >= lowNominal: 

把更多的視覺,你正在檢查這一點:

nominalValue range 
|-----x-----| 

seriesInput range, never going to be inside the nominalValue range 
    |----y----| 

seriesInput value, within range of nominalValue 
|-----x--y--|