我編寫了這段代碼,它應該比較一組行值和名義值。用戶應該輸入一個百分比值,將比較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"
您可以包括在你的問題中運行一個程序的例子? – 2014-10-06 21:27:43
Python有一個很好的比較語法:'1 <=容差<= 20'意味着與您爲檢查容差數而編寫的內容相同。 – 2014-10-06 21:34:56
呃,你是通過percentHigh(和Low)乘以比較的兩邊 - 你不需要這麼做... – 2014-10-06 21:35:54