Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>No what you say is not correct. Python is dynamically typed that means that while variables do <em>not</em> have types, its values do have types. You can easily check that by using the <code>type</code> function to get a value’s type:</p> <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; x = 1 &gt;&gt;&gt; type(x) &lt;class 'int'&gt; &gt;&gt;&gt; x = '1' # storing a different value in x &gt;&gt;&gt; type(x) &lt;class 'str'&gt; &gt;&gt;&gt; type(1) # type() works on the value, so this works too &lt;class 'int'&gt; &gt;&gt;&gt; type('1') &lt;class 'str'&gt; </code></pre> <p>Python is however also strongly typed, that means that no dynamic type conversions are made. This is the reason that you have to convert string explicitely to numbers when performing arithmetic operations:</p> <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; 1 + '1' # int does not allow addition of a string Traceback (most recent call last): File "&lt;pyshell#7&gt;", line 1, in &lt;module&gt; 1 + '1' TypeError: unsupported operand type(s) for +: 'int' and 'str' &gt;&gt;&gt; '1' + 1 # on the other hand, int cannot be converted to str implicitely Traceback (most recent call last): File "&lt;pyshell#6&gt;", line 1, in &lt;module&gt; '1' + 1 TypeError: Can't convert 'int' object to str implicitly </code></pre> <h3>edit</h3> <p>To respond to the code you posted, your if is like this:</p> <pre class="lang-py prettyprint-override"><code>if choice == 'A' or 'a': </code></pre> <p>This checks, if <code>choice == 'A'</code> (if <code>choice</code> equals to <code>'A'</code>), <strong>or</strong> if <code>'a'</code> evaluates to <code>True</code>. As <code>'a'</code> is a non-empty string it will always evaluate to true, so the condition is always true. What you want to write is this:</p> <pre class="lang-py prettyprint-override"><code>if choice == 'A' or choice == 'a': </code></pre>
 

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