2

Hello everyone I have a question. Why doesn't the following code dont produce any output or errors?

This is a code that encrypts and decrypts user codes

alpha = ['a','b','c','d','e','f',
 'g','h','i','j','k','l',
 'm','n','o','p','q','r',
 's','t','u','v','w','x',
 'y','z']
shift = range(26)
def user_info():
 info = input("\nPress 'e' to encrypt or 'd' to decrypt: ").lower()
 if info == 'e' or 'd':
 return info
def user_message():
 code = input("What is your message?: ")
 return code
def user_shift():
 shift = int(input("What is your shift number?: "))
 while True:
 if shift == int(shift):
 return shift
def True_Message(info, code, shift):
 if info[0] == 'd': #This encrypts the code 
 shift = -shift 
 for letter in code:
 if letter in alpha:
 alpha_2 = ord(letter) + shift
 secret_message = ""
 if alpha_2 in range (0, len(alpha)):
 final_mix = chr(alpha)
 secret_message += final_mix
return secret_message
info = user_info()
code = user_message()
shift = user_shift()
print(True_Message(info, code, shift))

There aren't any errors so I don't know what is wrong. I'm new at this encryption stuff, I'm not sure if this is even on the right track, any ideas? Thank you.

UPDATE!!!!!

Sorry I'm expecting the code to be able to encrypt and decrypt codes based on users choice

asked Mar 24, 2013 at 0:27
3
  • What is the current code doing and what are you expecting it to do? Commented Mar 24, 2013 at 0:29
  • You have some indentation problems in True_Message... could you fix those? Commented Mar 24, 2013 at 0:31
  • 1
    you have 2 indention issues. but presuming those are not the problem, this line: secret_message = "" is likely killing the output. Check the value of alpha_2 after that line, see if it is in the range you are expecting. otherwise, your secret message is going to be blank Commented Mar 24, 2013 at 0:42

4 Answers 4

3

You need to check your indentation and your usage of the or operator:

alpha = ['a','b','c','d','e','f',
 'g','h','i','j','k','l',
 'm','n','o','p','q','r',
 's','t','u','v','w','x',
 'y','z']
shift = range(26)
def user_info():
 info = input("\nPress 'e' to encrypt or 'd' to decrypt: ").lower()
 if info in ('e', 'd'): # 'or' does not work how you think it does
 return info
def user_message():
 code = input("What is your message?: ")
 return code
def user_shift():
 while True:
 shift = int(input("What is your shift number?: "))
 if shift == int(shift):
 return shift
def True_Message(info, code, shift):
 if info[0] == 'd': #This encrypts the code 
 shift = -shift 
 for letter in code:
 if letter in alpha:
 alpha_2 = ord(letter) + shift
 secret_message = ""
 if alpha_2 in range (0, len(alpha)):
 final_mix = chr(alpha)
 secret_message += final_mix
 return secret_message
answered Mar 24, 2013 at 0:37
Sign up to request clarification or add additional context in comments.

Comments

1

Python is an indentation-sensitive language. You have indentation errors all over the place.

How can you say "There aren't any errors" - are you running the script? The python interpreter will tell you exactly where they are. When I run your script:

C:\Users\Jonathon\temp>python caesar.py
 File "caesar.py", line 22
 return shift
 ^
IndentationError: expected an indented block

So your first error is here:

def user_shift():
 shift = int(input("What is your shift number?: "))
 while True:
 if shift == int(shift):
 return shift # ---> Should be indented one more level
answered Mar 24, 2013 at 0:31

4 Comments

Wouldn't this throw an error? (they said they didn't get any errors)
@Xymostech Yes. Just finished adding that bit.
@JonathonReinhart he probably entered the code into the SO editor wrong.
My apologies. While I did indent return shift in my code I didn't show that here.
1

As others say, you have some indentation problem.

For the logic level, you should definitively check if secret_message = "" is what you want. Other points:

  • What if the letter is Z and you add a shift ? (Check the ASCII table)
  • Are you sure you want the shift to be a range(26) in first place ? Try print(list(range(26))) to see what is going on with the range.

And don't discourage yourself, playing with the ASCII can be frustrating at first but once familiarized it can be very fun !

answered Mar 24, 2013 at 0:53

Comments

-3

Nah, check this out.

from tkinter import *
 #Functions. The first three do the job. The rest are about GUI
def wordlist(a):
 word_list=(list(a))
 return (word_list)
def length(a):
 return(len(wordlist(a))-1)
def encrypt(a,b):
 thelist=wordlist(a)
 i=0
 while i<=(length(a)):
 thelist[i]=(chr(ord(wordlist(a)[i])+(b)))
 i=i+1
 return (''.join(thelist))
def callback(): 
 e.focus_set()
 a=e.get()
 e2.focus_set()
 b=int(e2.get())
 e3.delete(0, (length(a)+1))
 e3.insert(0,(encrypt(a,b)))
 #User Interface
root = Tk(className="_Caesar Cipher Encryptor")
frame1 = Frame(root)
frame1.pack()
frame2= Frame(root)
frame2.pack()
frame3=Frame(root)
frame3.pack()
enter=Label(frame1, text="Enter Text:", bd=3, font="Ariel")
enter.pack(side=LEFT,fill=X)
shift=Label(frame2, text="Shift:", font="Ariel")
shift.pack(side=LEFT,fill=X)
result=Label(frame3,text="Encrypted Code:", font="Ariel")
result.pack(side=LEFT)
e= Entry(frame1,width=50, font="Ariel")
e.pack(side=RIGHT)
e2= Entry(frame2,width=10, font="Ariel")
e2.pack(side=LEFT)
e3=Entry(frame3, font="Ariel")
e3.pack(side=RIGHT)
b = Button(frame2, text="Encrypt", width=10, command=callback, font="Ariel")
b.pack(side=RIGHT)
mainloop()

1 Comment

Welcome to StackOverfow! It's always a good idea to accompany code with a description what the code is supposed to be doing and how it solves the problem presented in the question. You can edit your answer to improve it.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.