Search code examples
python-3.xsyntax-errorcaesar-cipher

caesar python 3 syntax-error when converting string parts to ascii code, calculate the new character and convert it back to encrypted text


I'm learning python3 on school after having had the basics of C programming. But now i've goten stuck on caesar form cs50. Though without use of the cs50 libraries.

So i've done the following:

Code:

print('ciphertext: ')

key = sys.argv[1]

for i in range(len(plain[i])

    if ord(plain[i]) > 64 and ord(plain[i]) < 91 or ord(plain[i]) > 96 and 
    ord(plain[i]) < 123

        ciphertext = chr(ord (plain[i]) + key %26)

print('ciphertext: {}'.format(ciphertext))

I get a syntax code on line 6 where ciphertext turned blue. Could someone tell me what i do wrong when i want to get the encrypted text out of this. Above this code i've defined plain.


Solution

  • There are a couple dozen syntax errors in your code Here fixed it for you try reviewing it

    import sys
    print('ciphertext: ')
    
    key = sys.argv[1]
    
    for i in range(len(plain[i])):
    if ord(plain[i]) > 64 and ord(plain[i]) < 91 and ord(plain[i]) > 96 and ord(plain[i]) < 123:
    
            ciphertext = chr(ord (plain[i]) + key %26)
    
    print('ciphertext: {}'.format(ciphertext))