I am currently trying to write a program, for school, in order to encrypt and decrypt a inputted message. I need the encrypted or decrypted message to only be in the alphabet no other symbols or keys, for example, with an inputted offset of 5 using the message van to encrypt, i want it to output 'afs'. Can anyone help please? This is my code currently:
def find_offset():offset = int(input("Enter an offset: "))if offset > 25 or offset < 0:print("Invalid offset, please enter another offset: ")find_offset()else:print("Okay")encrypt_fun(offset)def encrypt_fun(offset):choice = ''while choice != '3':choice = input("\nDo you want to encrypt or decrypt the message?\nEnter 1 to Encrypt, 2 to Decrypt, 3 to Exit Program: ")if choice == '1':message = input("\nEnter the message to encrypt: ")for i in range(0, len(message)):result = chr(ord(message[i]) + offset)print(result, end=''),elif choice == '2':message = input("\nEnter the message to decrypt: ")for i in range(0, len(message)):result = chr(ord(message[i]) - offset)print(result, end=''),elif choice != '3':print("You have entered an invalid choice. Please try again.\n\n")find_offset()
Answer
Currently you don't do any bounds checking for if you go over or under the ordinal value of a (97) or z (122).
If you were to specify these conditions, and check when adding or subtracting your offset, you would find the result you're looking for.
LOWER_BOUND = 97
UPPER_BOUND = 122def alter_char(char, offset):"""Alter char will take a character as input, change the ordinal value as per the offset supplied, and then perform some bounds checks to ensureit is still within the `a <-> z` ordinal range. If the value exceedsthe range, it will wrap-around to ensure only alphabetic charactersare used.:param str char: The character you wish to manipulate:param int offset: The offset with which to alter the character:return: Returns the altered character:rtype: str"""char_ord = ord(char) + offsetif char_ord > UPPER_BOUND:return chr(LOWER_BOUND + (char_ord - UPPER_BOUND) - 1)if char_ord < LOWER_BOUND:return chr(UPPER_BOUND - (LOWER_BOUND - char_ord) + 1)return chr(char_ord)
With that in place, you can alter your result = chr(ord(message[i]) + offset) lines to call the alter_char function to ascertain the character you're looking for, ie.
result = alter_char(message[i], offset) # for encryption
result = alter_char(message[i], -offset) # for decryption
# Prefixing the offset with a `-` is functionally equivalent to multiplying
# the offset by `-1`
As an aside, strings are naturally iterable, so your for i in range loop can be refactored as such
for char in message:result = alter_char(char, offset)
or going a step further and turning it into a list comprehension
result = ''.join([alter_char(char, offset) for char in message])
Just note, that this will still only cater for lower case messages being submitted for encryption and decryption as UPPER CASE letters have different ordinal values.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…
Im trying to add a digital clock to my Kivy program, it seems to be having trouble.Here is the .py:import kivykivy.require(1.10.0)from kivy.lang import Builder
from kivy.uix.screenmanager import Screen…
Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 4…
My dataframe is composed of accounting variables and a dummy variable that allows me to identify two types of company.
I would like to perform a t-test for every column of my dataframe in order to comp…
The last line of the output is none can someone explain why pls
def just_lyrics():print ("i am a bad coder")print (" i keep trying to learn everday")def double_lyrics():just_lyrics(…
I am struggling with datetime64 comparisons in dataframes to update a column. lets say we have a dataframe df with a date columndf.date.values[0]
Out[128]: numpy.datetime64(2015-05-17T22:00:00.00000000…
Trying to make the sample flask application more modular,I am new to python and flask trying to build a sample application where , I have planned to maintain the folder structure of the application a…
Im using the same sparql statement using two different clients but both are not returning the same results. The owl file is in rdf syntax and can be accessed here.
This is the sparql statement: PREFIX …