How do I change a value of a variable using an if else statement?
<p>I've been on it for days but nothing is helping. The value of smallest does not change no matter what i do. it does not happen with the value of largest although it uses almost the same line of code.</p>

<pre><code>smallest = None
largest = None
while True:
    num = raw_input("Enter a number: ")
    if num == "done":
        break
    try:
        x = int(num)
        if x &lt; smallest:
            smallest = x
        elif x &gt; largest:
            largest = x
    except:
        print"Invalid input"
        continue
print "Maximum is", largest
print "Minimum is", smallest
</code></pre>

<p>The problem is that you are, in both cases, comparing numbers to <code>None</code> in the first iteration. In Python 2, every number will come out as "greater" when compared to <code>None</code>, thus the code works for finding the maximum, but fails for finding the minimum.</p>

<p>BTW, in Python 3, the same would give you a <code>TypeError</code>.</p>

<p>To fix it, you can change your comparison to something like this, taking the <code>None</code> case into account:</p>

<pre><code>if smallest is None or x &lt; smallest:
</code></pre>

python python-2.7 python-3.x