Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Why do i get an invalid syntax error on line 10 and 12 ?

# init
password_out = ''
case_change = ord('a') - ord('A')
encryption_key = (('a','m', ('b','h'), ('c','t'),('d', 'f'), ('e', 'g'),('f', 'k'),
    ('g', 'b'), ('h', 'p'), ('i', 'j'), ('j','w'), ('k','e'), ('l','r'),
    ('m', 'q'), ('n','s'), ('o','l'), ('p','n'), ('q','i'), ('r','u'), ('s','o'),
    ('t','x'), ('u','z'), ('v','y'), ('w','v'), ('x','d'), ('y','c'), ('z','a'))

# program greeting
print("This program will encrypt and decrypt user passwords\n")

# get selection (encrypt/decrypt)
which = input('Enter (e) to encrypt, (d) to decrypt')
while which != 'e' and which != 'd':
    which = input('\nINVALID - Enter 'e' (e) to encrypt, (d) to decrypt')
encrypting = (which == 'e')

# get password
password_in = input('Enter your password: ')

# perform encryption / decryption
if encrypting:
    from_index = 0
    to_index = 1
else:
    from_index = 1
    to_index = 0

case_changer = ord('a') - ord('A')

for ch in password_in:
    letter_found = False

    for t in encryption_key:
        if ('a' <= ch and ch <= 'z') and ch == t[from_index]:
            password_out = password_out + t[to_index]
            letter_found = True
        elif('A' <= ch and ch <='Z') and chr(ord(ch) + 32) == t[from_index]:
            password_out = password_out + chr(ord(t[to_index]) - case_changer)
            letter_found = True

    if not letter_found:
        password_out = password_out + ch

# output
if encrypting:
    print('Your encrypted password is {}'.format(password_out))
else:
    print('Your decrypted password is {}'.format(password_out))

Can you show the whole error message?

I tried to run it on Python 3.5.2 shell, and it pop up an 'invalid syntax' and highlighted print on line 10.

2 Answers

The syntax error is in how you wrote out encryption key. Sometimes the actual syntax error is upstream of where the message says it is. Try this. The first two-letter tuple was not closed with a ')', and it appears that the rows were not flush with the start of the encryption_key tuple. Try this:

encryption_key = (('a', 'm'), ('b', 'h'), ('c', 't'), ('d', 'f'), ('e', 'g'),
                  ('f', 'k'), ('g', 'b'), ('h', 'p'), ('i', 'j'), ('j', 'w'),
                  ('k', 'e'), ('l', 'r'), ('m', 'q'), ('n', 's'), ('o', 'l'),
                  ('p', 'n'), ('q', 'i'), ('r', 'u'), ('s', 'o'), ('t', 'x'),
                  ('u', 'z'), ('v', 'y'), ('w', 'v'), ('x', 'd'), ('y', 'c'),
                  ('z', 'a'))

I get it now. Thanks a lot Peter, i appreciate your help =)

Hi there,

As well as what Peter has suggested, this line:

which = input('\nINVALID - Enter 'e' (e) to encrypt, (d) to decrypt')

could be a problem. Here, you close the string with the single quote surrounding the e, then open it again, leaving a letter e just hanging there? Maybe use double quotes to start and finished and/or escape the `'e' single quotes?

which = input("\nINVALID - Enter 'e' (e) to encrypt, (d) to decrypt")

(I don't know how to escape in Python - I'm sure Google does).

I hope that helps a little, :+1:

Steve.

To clarify ... you start with the code then the string opens and closes:

which = input('\nINVALID - Enter '

Then comes a lonely e, then the last of the string is written:

' (e) to encrypt, (d) to decrypt')

Good catch, Steve! Backslashes escape the next character, so this would work too:

which = input('\nINVALID - Enter \'e\' (e) to encrypt, (d) to decrypt')

That's actually an extra string that i miss typed it there. Thanks Steve, i appreciate your help =)

Same as most languages (that I'm familiar with), then! Thanks. :+1:

Would the distinction work between double & single quotes too?

No problem! :+1: :smile: