-2

I have made a caesar cipher solver in python, but there are no errors, it's just not printing anything. I have done similar things, but this hasn't happened before.

with open("C:\\Users\\Rajive\\AppData\\Local\\Programs\\Python\\Python3.4.3\\brit-a-z.txt", 'r') as f:
    check_list = [x.strip() for x in f.readlines()]

def crackcaesar(encrypted):
    for i in range(26):
        totalwords = 0
        numright = 0
        answer = []
        if i != 0: 
            for symbol in encrypted:
                if symbol.isalpha():
                    neword = (ord(symbol) + i)
                    if symbol.isupper():
                            if neword > ord('Z'):
                                neword -= 26
                            elif neword < ord('A'):
                                neword += 26
                    elif symbol.islower():
                         if neword > ord('z'):
                             neword -= 26
                         elif neword < ord('a'):
                             neword += 26
                    newletter = chr(neword)
                    answer.append(newletter)
                    for word in str(answer):
                        totalwords += 1
                        for line in check_list:
                            if line == word:
                                numright += 1
        if (numright // 2) > (totalwords // 2):
            print(str(answer))

print("Type your encoded caesar cipher message")
while True:
    crackcaesar(input())
Zags
  • 37,389
  • 14
  • 105
  • 140
Rohit Jose
  • 209
  • 1
  • 9

2 Answers2

1

The problem is that numright will never be greater than totalwords. Try

if numright == totalwords:
    print(str(answer))

Also, answer is a list of characters. str(answer) will give you "['a', 'b', 'c']". You need to use "".join(answer).

Zags
  • 37,389
  • 14
  • 105
  • 140
0

First, you have a 3-level nested loop, and not actually implementing the cipher. Second, you have no cipher key (see edit below). This for loop:

for word in str(answer):
                        totalwords += 1
                        for line in check_list:
                            if line == word:
                                numright =+ 1

Should not be nested in this already nested for loop:

 for i in range(26):
        totalwords = 0
        numright = 0
        answer = []
        if i != 0: 
            for symbol in encrypted:

Also, the if i != 0: inside of the loop where i goes from 0-25 doesn't seem to make sense. You seem to be trying to match the alphabet range, but you won't have much luck implementing a Caeser Cipher if you restrict your range from 0-26, as the first printable character starts at 32 (the space), and lowercase 'z' is 122. You should remove that loop all together - it ruins the cipher. Just use the for symbol in encrypted: loop.

Also, the loop for word in str(answer): is evaluating answer character by character, so the variable named totalwords is actually counting characters, which you could do simply by getting the length of the string. If you were trying to do words, you should call str.split(' ') on the answer variable, assuming the space character is the deliminating character. Hope this helps.

edit

Where is your key? (exactly). The main problem with your cipher is you are adding a counter variable to each ordinal value, when you need to add (or subtract) the key amount. Since you're not implementing key-shifting, you only need one value to add, otherwise you could implement the caeser cipher with an array of keys.

darrahts
  • 365
  • 1
  • 10
  • Sorry, I am still a newbie coder, do you mind explaining what you mean a bit more?, and to answer your question about the key, I was trying to test all possible outcomes with the i in range() function. – Rohit Jose Aug 20 '18 at 20:38
  • you can check out https://inventwithpython.com/chapter14.html for more information on the cipher, however what exactly do you need explained more? You only need one for loop to do the encrypting or decrypting, it does not need to be in a 3-tier loop. You only need to use one key value (also called a shift, since you "shift" the ordinal by this value). – darrahts Aug 20 '18 at 20:57
  • I finger fumbled the keyboard and pressed enter too soon – darrahts Aug 20 '18 at 21:09