Incrementing/decrementing values in a list given specified range (Very green at Python)

Question:

I am working on an assignment for an introductory Python class and the objective is to prompt the user for a password, determine it fits the given criteria, then encrypt the password by converting the characters to decimal, incrementing the letters and numbers, and decrementing the characters "!@#$" then return the new encrypted password. EX. an input of "123ABCabc$" should output "234BCDbcd#" **EDIT -> fixed this output per @gimix

I have everything up to the step of incrementing/decrementing and this last step has me pulling my hair out. I have tried a gang of different things but the two closest to successful attempts I have are as follows.

-in the first sample new_input is the users original input

-in the second sample use_ascii is new_input converted to decimal

1)

def encrypt(new_input):
    encrypted = ''
    for char in new_input:
        if chr(ord(char)) in list(range(97,123)): #ive tried different formatting options, thus the inconsistency 
            encrypted += chr(ord(char) + 1)
        if chr(ord(char)) > str(64):
            encrypted += chr(ord(char) + 1)
        if chr(ord(char)) >= str(48):
            encrypted += chr(ord(char) + 1)
        if chr(ord(char)) == str(64):
            encrypted += chr(ord(char) - 1)
        if chr(ord(char)) >= str(32):
            encrypted += chr(ord(char) - 1)
    return encrypted

def encrypt(use_ascii):
    encr = ''
    list1 = list(range(97,123))
    list2 = list(range(48,58))
    list3 = list(range(56,91))
    list4 = list(range(30,35))
    for i in use_ascii:
        if i in list1:
            encr = i + 1
        if i in list2:
            encr = i + 1
        if i in list3:
            use_ascii = i + 1
        if i in list4:
            use_ascii = i - 1
    return encr

then my main statements are (swapping ‘use_ascii’ for ‘new_pass’ when i test different options)…

new_input = input('Please input a password: n')
new_encr = encrypt(use_ascii)
print('Your encrypted password is: {}'.format(encrypt(use_ascii)))

**EDIT per Jasmijn

Sample 1 outputs: just errors and I havn’t been trying to fix it as much as I suspect that is the less-correct way to proceed

Sample 2 outputs: Your encrypted password is: 100

This is dec for the letter ‘d’ which suggests it is selecting the last character which fits the criteria of the first ‘if’ statement and incrementing it. I need it to increment each character and output the new decimal based on the ranges provided.

Asked By: yakubs

||

Answers:

The easiest way is to use the isalnum() string method to distinguish between letters/numbers an special characters. So you could simply do:

def encrypt(instring):
    outstring = ''
    for ch in instring:
        outstring += chr(ord(ch)+1) if ch.isalnum() else chr(ord(ch)-1)
    return outstring

Or, if you want a one-liner:

def encrypt(instring):
    return ''.join((chr(ord(ch)+1) if ch.isalnum() else chr(ord(ch)-1) for ch in instring))
Answered By: gimix
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.