Change Letters to numbers (ints) in python
<p>This might be python 101, but I am having a hard time changing letters into a valid integer.</p>

<p>The put what I am trying to do simply</p>

<p>char >> [ ] >> int</p>

<p>I created a case statement to give me a number depending on certain characters, so  what I tried doing was</p>

<pre><code>def char_to_int(sometext):
    return {
            'Z':1,
            'Y':17,
            'X':8,
            'w':4,
            }.get(sometext, '')
</code></pre>

<p>Which converts the letter into a number, but when I try using that number into any argument that takes ints it doesn't work.</p>

<p>I've tried </p>

<pre><code>text_number = int(sometext)
</code></pre>

<p>But I get the message TypeError: int() argument must be a string or a number, not 'function'</p>

<p>So from there I returned the type of sometext using </p>

<pre><code>print(type(sometext))
</code></pre>

<p>And the return type is a function.</p>

<p>So my question is, is there a better way to convert letters into numbers, or a better way to setup my switch/def statement</p>

<p>Heres the full code where its call</p>

<p>if sometext:
       for i in range ( 0, len(sometext)):
            char_to_int(sometext[i])</p>

<p>I've managed to get it working, ultimately what I changed was the default of the definition, I now set the definition to a variable before instead of calling it in another function, and I recoded the section I was using it.</p>

<p>Originally my definition looked liked this</p>

<pre><code>def char_to_int(sometext):
return {
        ...
        }.get(sometext, '')
</code></pre>

<p>But I changed the default to 0, so now it looks like</p>

<pre><code>def char_to_int(sometext):
return {
        ...
        }.get(sometext, 0)
</code></pre>

<p>The old code that called the definition looked</p>

<pre><code>if sometext: 
          for i in range ( 0, len(sometext)): 
                    C_T_I = int(char_to_int(sometext[i]))
</code></pre>

<p>I changed it to this.</p>

<pre><code>if sometext: 
          for i in range ( 0, len(sometext)): 
                    C_T_I = char_to_int(sometext[i])
                    TEXTNUM = int(C_T_I)
</code></pre>

<p>Hopefully this clarifies the changes. Thanks for everyone's assistance.</p>

<p>in the python console:</p>

<p><code>&gt;&gt;&gt; type({ 'Z':1, 'Y':17, 'X':8, 'w':4, }.get('X', ''))
&lt;class 'int'&gt;</code></p>

<p>so as cdarke suggested, you should look at how you are calling the function.</p>

python switch-statement