我编写了这段代码,它应该比较一组行值和名义值。用户应该输入一个百分比值,将比较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