HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Caesar cipher with GUI

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withcaesarguicipher

Problem

I have created a functional Caesar cipher in Python 3 and I have implemented a GUI using Tkinter.

```
def MOST_COMMON_LETTER():
return("e")

def circle(l,n):
if n < (len(l) - 1):
return(l[n])
else:
return(l[n % (len(l))])

def most_frequent(l): # not whitespace
l = l.replace(' ','')
all_frequencies = [l.count(i) for i in l]
for i in l:
if l.count(i) == max(all_frequencies):
return(i)

def my_formatting(string):
for i in string:
if i in ".,1234567890?!;:_;-/()[]{}+-*\"'":
string = string.replace(i,'')
string = string.lower()
return(string)

def en_de_code(string,key,en_or_de): #calling with en is encode, calling with de is decode
alphabet = "abcdefghijklmnopqrstuvwxyz"
new_string = ""
string = my_formatting(string)
for i in string:
if i == " ":
new_string += " "
else:
position = alphabet.index(i)
if en_or_de == 'en':
new_string += circle(alphabet,position + key)
elif en_or_de == 'de':
new_string += circle(alphabet,position - key)
return(new_string)

def encode(string,key):
return(en_de_code(string,key,"en"))

def decode(string,key):
return(en_de_code(string,key,"de"))

def randomly_encode(string):
import random
key = random.randint(1,25)
return(encode(string,key))

def brute_force_decode(string):
new_string = ""
for key in range(1,26):
new_string += decode(string,key)
new_string += '-'
return(new_string)

def meaningful_decode(string):
alphabet = "abcdefghijklmnopqrstuvwxyz"
the_most_frequent = most_frequent(string)
key = -(alphabet.index(the_most_frequent) - alphabet.index(MOST_COMMON_LETTER()))
return(decode(string,key))

def GUI():
try:
import Tkinter as tk
except:
import tkinter as tk

master = tk.Tk()

master.wm

Solution

UI issues

  • It's not obvious where the input text box is. It has no border or colour difference until it gains focus (which is difficult if you don't know where it is).



  • It's not obvious where the output is. I would expect the output to be as prominent as the input — after all, it will contain just as much text. I would also want it to be a read-only text area so that I can select text and copy it to the clipboard.



  • The buttons could be laid out better than than in a one-column arrangement.



Algorithm

en_de_code() is an awkward function name, and en_or_de is an awkward parameter. I would just define decode() in terms of encode(). The interpretation that comes to mind is "English or German?"

def encode(string, key):
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    new_string = ""
    string = my_formatting(string)
    for i in string:
        if i == " ":
            new_string += " "
        else:
            position = alphabet.index(i)
            new_string += circle(alphabet, position + key)
    return new_string

def decode(string, key):
    return encode(string, -key)

Code Snippets

def encode(string, key):
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    new_string = ""
    string = my_formatting(string)
    for i in string:
        if i == " ":
            new_string += " "
        else:
            position = alphabet.index(i)
            new_string += circle(alphabet, position + key)
    return new_string

def decode(string, key):
    return encode(string, -key)

Context

StackExchange Code Review Q#65210, answer score: 6

Revisions (0)

No revisions yet.