Alan Casanova
Alan Casanova

Reputation: 3

Variable within a number

This code ask for a message and a value to the user and then it modifies it with the given value. The problem is that I want the ASCII codes to not go over 126 or under 33, I tried to do so in the highlighted part of the code but when the value of the ASCII code gets over 126 the code returns me nothing for some reason.

loop = True
def start():
    final_word = []
    word_to_crypt = str(raw_input("Type a word: "))
    crypt_value = int(raw_input("Choose a number to cript yout message with: "))
    ascii_code = 0
    n = 0
    m = len(word_to_crypt)
    m = int(m - 1)
    while n <= m:
        ascii_code = ord(word_to_crypt[n])
        ascii_code += crypt_value
############# highlight  ############# 
        if 33 > ascii_code > 126:
            ascii_code = (ascii_code%94)+33
    ############# highlight  #############
    final_word.append(chr(ascii_code))
        n += 1
    print 'Your crypted word is: ' + ''.join(final_word)

while loop:
    start()

Sorry if it's not formatted well or for any mistakes in my explanation but I'm on my phone and I'm not native

Solved thank you very much this site and this community is helping me a lot!

Upvotes: 0

Views: 22

Answers (1)

Christian Tapia
Christian Tapia

Reputation: 34156

There is no number that is greater than 126 and less than 33 at the same time. It should be:

if 33 < ascii_code < 126:

Edit:

If you want the reversed case, you will have to do it separately:

if ascii_code < 33 or ascii_code > 126:

Or you can just use the in operator and a list:

if ascii_code not in [33,126]:

Upvotes: 1

Related Questions