Reject user input if criteria not met in Python
<p>I know this question is similar to one I have already asked, but it is an extension and so justified its own space :-)</p>

<p>I am a Python newbie writing a code which takes input from a user and then stores that user input in an array (to do more stuff with later), provided two criteria are met:</p>

<p>1) The total inputs add up to one</p>

<p>2) There is no input itself greater than one.</p>

<p>I have already had <a href="http://stackoverflow.com/questions/39956782/reject-or-loop-over-user-input-if-two-conditions-not-met">some help with this question</a>, but had to modify it a bit since my code inputs can't easily be written with the inputs being classified by some index "n" (the questions prompting input can't really be formatted as "input (n), where n runs from 1 to A")  </p>

<p>Here is my attempt so far:</p>

<pre><code>num_array = list()
input_number = 1
while True:

     a1  = raw_input('Enter concentration of hydrogen (in decimal form): ')
     a2 = raw_input('Enter concentration of chlorine (in decimal form): ')
     a3 = raw_input('Enter concentration of calcium (in decimal form): ')



     li = [a1, a2, a3]

     for s in li:
        num_array.append(float(s))
        total = sum([float(s)])

     if float(s-1)  &gt; 1.0:     
         num_array.remove(float(s-1))
         print('The input is larger than one.')

     continue


     if total &gt; 1.0:    # Total larger than one, remove last input and print reason
         num_array.remove(float(s-1))
         print('The sum of the percentages is larger than one.')

     continue

     if total == 1.0:    # if the sum equals one: exit the loop
          break

input_number += 1
</code></pre>

<p>I am rather glad it compiles, but Python doesn't like the line </p>

<pre><code>if float(s-1)  &gt; 1.0: 
</code></pre>

<p>for which it throws the error:</p>

<pre><code>TypeError: unsupported operand type(s) for -: 'str' and 'int'
</code></pre>

<p>I know this is because the "s" is a string, not an integer, but I can't think of an easy way around the problem, or for how to implement a loop over user inputs in this case in general.</p>

<p>How to improve this program to only write user input to the array if the criteria are met?</p>

<p>Thanks for your time and help!</p>

<p>You'll simply need to cast to float before you do subtraction:</p>

<pre><code>if float(s) - 1  &gt; 1.0:
</code></pre>

<p>This way, you can subtract 1 from the float value of <code>s</code></p>

<p><strong>EDIT:</strong>   I would also make the following changes to your code in order to function more correctly.</p>

<pre><code>num_array = list()
input_number = 1
while True:
    a1  = raw_input('Enter concentration of hydrogen (in decimal form): ')
    a2 = raw_input('Enter concentration of chlorine (in decimal form): ')
    a3 = raw_input('Enter concentration of calcium (in decimal form): ')

    try:   # try to cast everythiong as float.  If Exception, start loop over.
        li = [float(a1), float(a2), float(a3)]
    except ValueError:
        continue

    total = 0  # reset total to 0 each iteration
    for s in li:
        num_array.append(s)
        total += s  # use += to keep running toatal

        if s &gt; 1.0:     
            num_array.remove(s)
            print('The input is larger than one.')
            break  # break rather than continue to break out of for loop and start while loop over


        if total &gt; 1.0:
            num_array.remove(s)
            print('The sum of the percentages is larger than one.')
            break   # break again

    if total == 1.0:
        break
</code></pre>

<p>I think this is what you were going for.</p>

python loops input