Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Almost the same function, but doesnt work?

i tried something using python and while the encrypt() function works fine, the decrypt() function doesnt give me any output, not even an error :(

My code:

import os
abc=['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', ' ', '.', ',', '1', '2', '3', '4',   '5', '6', '7', '8', '9', '0', '+', '-', ':', "'"]
mixed=abc[::-1]
os.system("clear")
def menu():
    print "-----------"
    print "[1] Encrypt"
    print "[2] Decrypt"
    print "-----------"
    if input(">>> ")==1:
        encrypt()
    elif input(">>> ")==2:
        decrypt()

def encrypt():
    os.system('clear')
    text=raw_input(">>> ").lower()
    text=list(text)
    textnew=text
    for i in range(len(text)):
        textnew[i]=mixed[abc.index(text[i])]
    print ''.join(textnew)
    menu()

def decrypt():
    os.system('clear')
    text=raw_input(">>> ").lower()
    text=list(text)
    textnew=text
    for i in range(len(text)):
        textnew[i]=abc[mixed.index(text[i])]
    print ''.join(textnew)
    menu()

menu()

2 Answers

if input(">>> ")==1:
    encrypt()
elif input(">>> ")==2:
    decrypt()

You're reading a second input in your elif. That's why it seems like the first command is being ignored. By the way, input in Python 2 is unsafe. You should stick with raw_input (which just returns a string without trying to evaluate it).

command = raw_input(">>> ")
if command=="1":
    encrypt()
elif command=="2":
    decrypt()
like image 182
khelwood Avatar answered Sep 30 '26 05:09

khelwood


Here's a few suggestions:

  • use raw_input
  • don't repeat yourself - no need for 4 print statements
  • Your functions print same prompt. This confuses the users. Add at least some indication as to what's happending
  • instead of recursively calling main() (and eventually getting to the limits how many function calls can be on stack), make main() have a loop that calls your functions each time.
  • get in the habit of writing if __name__ = '__main__': since when you start writing Python modules, you don't want the script run all the things on import, but rather just provide the functions to the calling script.
  • wrap lines at 80 characters

Edited code:

import os
abc=['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', ' ', '.', 
     ',', '1', '2', '3', '4',   '5', '6', '7', '8', '9', '0', '+', '-', 
     ':', "'"]
mixed=abc[::-1]
os.system("clear")

def menu():
    header = '\n'.join(["-----------","[1] Encrypt",
                        "[2] Decrypt","-----------"])
    while True:
        print header
        user_input = raw_input(">>> ")
        # print "DEBUG:user input:",user_input
        if user_input == '1':
            encrypt()
        elif user_input == '2':
            decrypt()
        elif user_input == 'q':
            exit()
        else:
            print("Bad input")

def get_input():
    os.system('clear')
    print "Encrypt"
    text=raw_input(">>> ").lower()
    return list(text)


def encrypt():
    text = get_text()
    textnew=text
    for i in range(len(text)):
        textnew[i]=mixed[abc.index(text[i])]
    print ''.join(textnew)

def decrypt():
    text = get_input()
    textnew=text
    for i in range(len(text)):
        textnew[i]=abc[mixed.index(text[i])]
    print ''.join(textnew)

if __name__ = '__main__':    
    menu()
like image 24
Sergiy Kolodyazhnyy Avatar answered Sep 30 '26 04:09

Sergiy Kolodyazhnyy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!