Okay, the encryption algorithm idea was stupid. But I still made an encryption program. I got the absolute most possible frustrating situation. The program doesn't work in the console window. It spits out a giant error (probably recursion judging how many errors it got), but it DOES work in IDLE. So the only way to fix it is manually see the error, which I've tried to do again and again with no success. So, the error comes after you input the message. So here's the code:

import os
import random

if os.name == "nt":
    os.system("color 48")
    os.system("title Encryptor/Decryptor")
    def clear():
        os.system("cls")
else:
    def clear():
        os.system("clear")

def menu():
    clear()
    print ("")
    print ("Encryptor/Decryptor")
    print ("-------------------")
    print ("1) Encrypt Message ")
    print ("2) Decrypt Message ")
    print ("-------------------")
    menu_choice = input ("Choose one: ")
    
    if menu_choice != "1":
        if menu_choice != "2":
            clear()
            print ("")
            print ("Please type a valid choice!")
            input ()
            menu()
    
    if menu_choice == "1": encrypt()
    if menu_choice == "2": decrypt()
    
def encrypt():
    clear()
    print ("")
    print ("------------------------------------")
    print ("1) Use computer generated number key")
    print ("2) Manually key                     ")
    print ("------------------------------------")
    encrypt_choice = input ("Choose one: ")
    
    if encrypt_choice == "1":
        key = list(str(random.random()).split(".")[1])
    else:
        clear()
        print ("")
        key = list(str(input("Please type key: ")))

    show = ""
    while 1:
        clear()
        print ("")
        print (show)
        print ("Type S to show key, H to hide key  ")
        print ("Type R to change key               ")
        print ("-----------------------------------")
        message = list(input ("Please type a message to be encrypted: "))

        if "".join(message).lower() == "s":
            show = str("Key is: " + str("".join(key)))
            continue
        if "".join(message).lower() == "r":
            encrypt()
        if "".join(message).lower() == "h":
            show = ""
            continue

        break


    while len(key) < len(message):
        key += key

    spaces = 0
    encrypt_list = []
    shift = 1
    for loop in range((len(message))):
        shift += 1
        if str(message[loop]) == ' ':
                spaces += 1
        else:
            if str(message[loop]) == '  ':
                spaces += 1
            ''
        if str(message[loop]) != ' ':
            if str(message[loop]) != '  ':
                if str(message[loop]) == str(key[loop-spaces]):
                    encrypt_list.append(message[loop])
                else:
                    if (shift % 2) == 0:
                        loop_ord = str(chr(ord(key[loop-spaces])+ord(message[loop])))
                    else:
                        if ord(str(message[loop])) < ord(str(key[loop-spaces])):
                            loop_ord = "9u" + str(chr(ord(key[loop-spaces])-ord(message[loop])))
                        else:
                            loop_ord = chr(ord(message[loop])-ord(key[loop-spaces]))
                    encrypt_list.append(loop_ord)

    print ("")
    print ("Encrypted message is: ", "".join(encrypt_list))
    if show == "":
        print ("")
        ask = input ("Show key before leaving(Y/N): ")
        if ask.lower() == "y":
            print ("")
            print ("Key is: " + str("".join(key)))
    input ()
    menu()

def decrypt():
    menu()

menu()

Help would GREATLY be appreciated.

Recommended Answers

All 9 Replies

Well already I see one problem. You are using the input() function instead of using raw_input(). Input only receives int data types. Though when you try to compare the user input to a valid choice you are comparing it to a string. Use raw_input() because it reads in strings instead and this should solve that problem.

Edit: Also you need not use so many print statements. You can print lines like so.

# using triple quoted strings

print '''What would you like to do?
1) This
2) Or this
3) Exit
'''

You are using the input() function instead of using raw_input().

He also is using parenthesis in his print statements. I'd surmise that he's using Python 3.0, in which case the use of input is the only option. ( input has been replaced by raw_input )

EDIT: Additionally, I tried his code (after modifying it to be Python 2.X compliant) and it worked for the most part. To the OP: could you explain the error in more detail and provide some traceback?

Wow I didn't catch that. I ran it using 2.5 and it seemed to work so I assumed.

I want to encourage posters to give us the version of Python they are using. Otherwise the rest of us have to assume and ask. Python25 for instance allows you to use the print statement or the print() function. So using the fact that print() was used is not necessarily indicative of the a Python3 application.

Not quite sure what the OP means with it DOES work in IDLE, unless IDLE uses Python3 and his system default on saved files is still Python2.

No, system default for files and IDLE are the same, I can't explain the error to you because the console window closes before I can see it, but IDLE gives no error.

Well already I see one problem. You are using the input() function instead of using raw_input().

Do you think this isn't my code? How could I make code like this if I didn't even know what function to use.
I hope you understand my problem now, I don't know why every thing is working in idle, but not the console.

Try this trick (I'm assuming you're using Windows)

1) Open up a terminal (command prompt window)

Ctrl+R -> cmd -> [Enter]

2) Type/find path to Python executable

dir C:\Py* -> [Enter]
# You should see the folder name for your Python install (I'm guessing it's Python30
C:\Python30\python.exe -> Drag-and-drop your python script to the command prompt window (where you're typing this stuff), which should auto-insert the full path to your python script -> [Enter]

This should get you running your python script inside a persistent window that won't close itself. And example command would be:

C:\> C:\Python25\python.exe "C:\Documents and Settings\Administrator\Desktop\em_test.py"

I wasn't trying to say you didn't write the code. It was just an error that popped up for me when I tried to run the code. Though jlm699 corrected me already. Anyways since the console is closing before you can see the error, open a console first and run the program from the command line. That way when the program throws the error the console wont close.

Thank you for the help. But the problem is solved, it wasn't the encrypting part (thank goodness), but when it attempted to print some of the unicode characters with 2 symbols it couldn't. Still don't know how IDLE found a way around it.

If you declared the encoding at the beginning of your program the same way that IDLE does, it would have worked I believe?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.