Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Make sure that <code>food</code> is not a string, because in CPython2, <code>'-5' &lt; 0</code> is False:</p> <pre><code>&gt;&gt;&gt; '-5' &lt; 0 False </code></pre> <p>You can test if this is the problem in your code by placing an assert before the if statement:</p> <pre><code>assert not isinstance(food, basestring) if food &lt; 0: ... </code></pre> <p>If <code>food</code> is a string (or unicode), the program stop with an <code>AssertionError</code> when it reaches the <code>assert</code> statement.</p> <p>If it does turn out that <code>food</code> is a string, you will need to track down the source of the problem. When you find where <code>food</code> is being defined as a string, you can convert that string into an int using the <code>int</code> function:</p> <pre><code>food = int(...) </code></pre> <p>Of course, you could even use <code>int</code> in the condition,</p> <pre><code>if int(food) &lt; 0: </code></pre> <p>but if <code>food</code> is intended to be an <code>int</code>, then this would be a sloppy way to program.</p> <hr> <p>From <a href="http://docs.python.org/2/library/stdtypes.html#comparisons" rel="nofollow">the CPython2 docs</a>, </p> <blockquote> <p>CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.</p> </blockquote> <p>So, since the word <code>'float'</code> comes before <code>'string'</code> alphabetically, all floats are ordered less than any string.</p> <p>In Python3, comparing objects of different type raises a TypeError, thus removing this pitfall.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload