Error type in my work about python
<pre><code>#!/usr/bin/env python
# coding=utf8
  value=input("please enter value:")
  result=hex(value)
  r=hex(0xffff-result)
  print r
  print result


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

<p>I study python for a few days,I try this python job,I can't understand what's the shape of '0xffff',it is str or int?and it's right for 'result' be str?</p>

<p>When you are just beginning with Python you can get a huge amount of help by just running an interactive Python session and typing code in. Just enter the command</p>

<pre><code>python
</code></pre>

<p>The code you enter can be expressions or statements. Expressions are automatically evaluated, and the result printed (unless it happens to be <code>None</code>). Statements are executed.</p>

<pre><code>&gt;&gt;&gt; value=input("please enter value:")
please enter value:2134
&gt;&gt;&gt; value
'2134'
</code></pre>

<p>The quotes around the value flag it as a string.</p>

<pre><code>&gt;&gt;&gt; result=hex(value)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: 'str' object cannot be interpreted as an integer
</code></pre>

<p>If you look at the <a href="https://docs.python.org/2/library/functions.html#hex" rel="nofollow">documentation for the <code>hex</code> built-in function</a> you will see that it takes an integer argument.</p>

<pre><code>&gt;&gt;&gt; hex(int(value))
'0x856'
</code></pre>

<p>So you  have now got a hexadecimal string. let's store it.</p>

<pre><code>&gt;&gt;&gt; result = hex(int(value))

&gt;&gt;&gt; r=hex(0xffff-result)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: unsupported operand type(s) for -: 'int' and 'str'
&gt;&gt;&gt; result
'0x856'
</code></pre>

<p>The problem here is that you are trying to subtract a hex <em>string</em> from a hex <em>integer</em>. Because <code>0xffff</code> is in fact an integer value. So all you actually need to do is subtract the (integer) value of your input and subtract it. Then you presumably want to convert the result to hexadecimal.</p>

<pre><code>&gt;&gt;&gt; 0xffff - int(value)
63401
&gt;&gt;&gt; hex(0xffff - int(value))
'0xf7a9'
</code></pre>

<p>By going through this interactive experimental methodology you save yourself considerable time by learning instantly what works and what doesn't work (and often, in the latter case, why not). So you are then much better placed to write your complete program.</p>

python