Cryptography with ASCII in python 3
<p>Hello there and thanks in advance.</p>

<p>i'm trying to making a cryptography program i have to do for school. i'm not a advanced python expert, so i'm sorry if this is a silly question. when i run this program and insert for example abc with shift 2 it will return cde which is good. but i tried to insert xyz aswell with shift 3 and instead of shifting correctly abc it returns aaa. this also happens if i use shift 2 then it returns zaa. how can i adjust my program to correctly start from the beginning when the alphabet is done with the ASCII tabel</p>

<pre><code>shift = int(input("Please insert a number you want to shift the characters with: "))

end = ""

for x in alf:
    ascii = ord(x)

if ascii &gt;= 97 and ascii &lt;= 122:
    res = ascii + shift
    if res &gt; 122:
        res = 0 + 97
        min = res + shift
    end = end + chr(min)

print (end)                                
</code></pre>

<p>It is because your logic expression is wrong. Here is an example that will allow any positive integer as right shift, beginning again at a again. And it can be very much optimized (hint: use modulo operator), but this is with minor changes to your code and numerics written out loud.</p>

<pre><code>for x in alf:
  ascii = ord(x)

  if ascii &gt;= 97 and ascii &lt;= 122:
    res = ascii + shift
    while res &gt; 122:
      res = res - (122 - 97) - 1
    end = end + chr(res)
</code></pre>

python python-3.x loops ascii