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
4 Answers 4
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
Comments
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
4 Comments
return shift in my code I didn't show that here.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
letterisZand you add a shift ? (Check the ASCII table) - Are you sure you want the shift to be a
range(26)in first place ? Tryprint(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 !
Comments
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()
True_Message... could you fix those?