Detect the input number of signs in python and use it in a loop for encryption
<p>I am writing a very easy encryption program. I just shift the ASCII order number of a sign with a key. My code looks as following:</p>

<pre><code>#Type in sign or text and key
clear_text = input("Text? ")
key = int(input("Key? "))

# saves ASCII order number of the sign
ord_number = ord(clear_text)

#shift ASCII odernumber with the key -&gt; encryption
shift_ord_number = ord_number + key
key_ord_number = chr(shift_ord_number)
print(key_ord_number)
</code></pre>

<p>I want to advance this code by being able to type in a whole sentence. For this I somehow must detect how many signs the user has typed in. How would that be possible? And what loops would you recommend?</p>

<p>Iterate over the string with the <code>for</code> loop and use your encryption logic with in the loop. </p>

<p>Making minimal changes to your original code, below is the sample code to achieve it:</p>

<pre><code>clear_text = input("Text? ")
key = int(input("Key? "))

new_list = []
for c in clear_text:
    # saves ASCII order number of the sign
    ord_number = ord(c)

    #shift ASCII odernumber with the key -&gt; encryption
    shift_ord_number = ord_number + key

    key_ord_number = chr(shift_ord_number)
    new_list.append(key_ord_number)

new_string = ''.join(new_list)
</code></pre>

<p>where <code>new_string</code> is the encrypted string.</p>

<p>Instead of <code>for</code> loop, you may also do <em>list comprehension</em> as:</p>

<pre><code>new_string = ''.join(chr(ord(c) + key) for c in clear_text)
</code></pre>

<p><em>Sample Example:</em></p>

<pre><code>&gt;&gt;&gt; clear_text = 'This is the sample code'
&gt;&gt;&gt; key = 3
&gt;&gt;&gt; ''.join(chr(ord(c) + key) for c in clear_text)
'Wklv#lv#wkh#vdpsoh#frgh'  # &lt;- value of "my_string"
</code></pre>

python