2014-10-06 46 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--|