0

I wrote this code and it's supposed to compare the set of line values with the nominal value. The user is supposed to input a percentage value that will compare the lineValue to the nominal value. If the lineValue is within the percentage given for the nominal value it will pass as true.

My program will only return true only if the lineValue number is exactly the nominal value. All other values are failures even if it is within the percentage that the user inputs. Does anyone see an error in my code that would prevent the numbers from being registered as 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"
3
  • 1
    Could you include in your question an example run of the program? Commented Oct 6, 2014 at 21:27
  • Python has a great syntax for comparisons: 1 <= tolerance <= 20 means the same thing as you have written for checking the tolerance number. Commented Oct 6, 2014 at 21:34
  • Er, you are multiplying both sides of your comparison by percentageHigh (and Low) -- you don't need to do that... Commented Oct 6, 2014 at 21:35

3 Answers 3

2

You've already computed highNominal and lowNominal, so you want this line:

if seriesInput <= highNominal and seriesInput >= lowNominal:           

or as @GregHewgill pointed out:

if lowNominal <= seriesInput <= highNominal:
Sign up to request clarification or add additional context in comments.

2 Comments

Again, lowNominal <= seriesInput <= highNominal means the same thing and might be easier to read.
@GregHewgill, easier to read but I always seem to type it wrong the first time.
1

In your check

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

you are checking if the range of the seriesInput is within the range around the nominal value, which by definition it will never be. You want to simply check if the value of the seriesInput is within the range around the nominalValue, like so

if seriesInput <= highNominal and seriesInput >= lowNominal:

Put more visually, you are checking this:

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

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

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

Comments

1

Your current code is asking:

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

But

highNominal = nominalValue*percentageHigh
lowNominal = nominalValue*percentageLow

So your comparison is equivalent to:

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

Which simplifies down to:

if seriesInput <= nominalValue and 
   seriesInput => nominalValue:

As should be clear from seeing it that way, that can only be true when seriesInput == nominalValue, as seriesInput can't be both larger and smaller than nominalValue!

2 Comments

That's not it. OP is comparing highNominal and lowNominal not nominalValue.
highNominal is nominalValue*percentageHigh. The comparison is (percentageHigh*seriesInput) <= highNominal which is equivalent to (percentageHigh*seriesInput) <= (nominalValue*percentageHigh), which when you divide by percentageHigh on both sides, gives you what I said.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.