2

For some reason when I run the code at the end it just displays the message without encrypting or decrypting it im really confused please do not hate on me if this is really obvious I am very new to python and barely know the basics

#declare variables
NewWord =""
NewLetter = ""
SecretMessage = 0

mode = input("Please enter a mode: ").lower()  #makes it lowercase
message = input("Please enter a message: ").lower() # lowercase to tackle capitals
while True:
    try:
        offset = int(input("Please enter a number: ")) #If no exception occurs, the except clause is skipped and execution of the try statement is finished.
        break
    except ValueError: #If exception occurs, the except clause continues printing not a valid number and letting you re-enter the offset and not just throwing up an error.
        print ("Not a valid number")
#print(mode, message, offset) #Test to check user Input

for letter in message : 
    SecretMessage = ord(letter)
    if mode == "Encrypt" :
        SecretMessage += offset # add the offset to the letter
    if mode == "Decrypt" :
         SecretMessage -= offset # subtract the offset to the letter
    if SecretMessage < 97:
       SecretMessage  += 26
    if SecretMessage > 122:
        SecretMessage -= 26
    NewLetter = SecretMessage 
    NewLetter = chr(NewLetter) 

   # print(newLetter)# check conversion
    NewWord += NewLetter

print(NewWord)
Artjom B.
  • 61,146
  • 24
  • 125
  • 222

2 Answers2

0
mode = input("Please enter a mode: ").lower()

makes the mode all lowercase which prevents it to equal either "Encrypt" or "Decrypt". So, your if clauses are not executed.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
0

You are lowercasing the mode variable, so there's no way it could equal Encrypt or Decrypt. You'd have to test for the lowercase versions of those words:

if mode == "encrypt" :
    SecretMessage += offset # add the offset to the letter
if mode == "decrypt" :
     SecretMessage -= offset # subtract the offset to the letter
Mureinik
  • 297,002
  • 52
  • 306
  • 350