0

We have to do a vigenere cipher for a project, and my code just keeps repeating itself. Like it wont run the encrypt or decrypt. here is my code. like this is what it does for an example..

"Hey There user!

whats your message??hi

How many letters are in the message?2

Do you want to decrypt or encrypt?decrypt

Lets decrypt your message!!

Do you want to decrypt or encrypt?"

print "Hey There user!"
def vig():
dore = raw_input("Do you want to decrypt or encrypt?")
if "decrypt" in dore: 
    print "Lets decrypt your message!!"
else:
    print "lets encrypt your message!!"


def dore(message):
encrypt = ''
decrypt = ''
if "encrypt" in vig(): 
 for i in range(0, len(message)):
    e = ord(message[i]) + ord(key[i%len(key)]) - 65
if e > 90:
    e -= 26
    encrypt += chr(e)
    print encrypt
if "decrypt" in vig():
    e = ord(message[i]) - ord(key[i%len(key)]) + 65
if e < 65:
    e += 26
    decrypt += chr(e)
    print decrypt

 ####################################
 ###########################################:)#####
 message = raw_input("whats your message??")
 key = raw_input("How many letters are in the message?")
 vig()
 dore(message)
 message = message
 encrypt = ''
 decrypt = ''
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

One of the first things you do in dore is call vig again:

if "encrypt" in vig():

Try separating encryption and decryption into two functions and calling them accordingly:

def vig(message):
    ui = raw_input("Encrypt or decrypt? ").lower()
    if "decrypt" in ui:
        return decrypt(message)
    else:
        return encrypt(message)

Also, the user doesn't need to enter the length of the message, just do:

key = len(message)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • now it's saying Traceback (most recent call last): File "C:/Python27/fehio.py", line 31, in vig(message) File "C:/Python27/fehio.py", line 25, in vig return encrypt(message) NameError: global name 'encrypt' is not defined – user3298288 Feb 24 '14 at 22:24
  • Well, yes; you will need to actually *define* those two functions `encrypt` and `decrypt` based on the two parts of your current function `dore`. – jonrsharpe Feb 25 '14 at 15:40