Error Capturing specific bounds
<p>hello python comunity,</p>

<p>How do I stop this from looping?</p>

<pre><code>error33 = int(2)
    while error33 &gt; 1:
        while True:
                try:
                    survivalrateforjuveniles = float(input("Please enter the survival rate for poomen"))##Float input is being used as
                    break
                except ValueError:
                    print("Please enter a number between 0 and 1")
                    continue
                else:
                    break
</code></pre>

<p>Many Thanks</p>

<p>This works...</p>

<pre><code>error33 = 2
while error33 &gt; 1:
        while True:
                try:
                    survivalrateforjuveniles = float(input("Please enter the survival rate for poomen\n"))
                    break
                except ValueError:
                    print("Please enter a number between 0 and 1")
                    continue
                else:
                    error33 = 0
                    break
</code></pre>

<p>I do not know exactly what you are doing... but I think I would use somehting similar to this:</p>

<pre><code>error33 = 2
survivalrateforjuveniles = -1.0
while error33 &gt; 1:
    while not (0 &lt; survivalrateforjuveniles &lt; 1):
        survivalrateforjuveniles = input("Please enter the survival rate for poomen\n")
        try:
            survivalrateforjuveniles = float(survivalrateforjuveniles)
        except ValueError:
            survivalrateforjuveniles = -1
        print("Please enter a number between 0 and 1")

    print('do what you want')
    error33 =0
</code></pre>

<p>Not so much different from your solution but you don't need the <code>break</code> and looks more robust. However, creating a function to read the <code>survivalrate...</code> and the other inputs (which I guess you have) would be much better since you would not use the same lines of code more than once (which is one of the basic practice to write good software).</p>

python error-handling