I am trying to call a new function called def importFile():
however i am getting the error saying that importFile()
isn't defined. What am i doing wrong, I know this is probably simple but I'm new to coding.
This is the section where I am trying to call the function:
def main():
myMode = input("Encrypt 'e' or Decrypt 'd': ")
textFile = input("Would you like to import a text file 'Y' or 'N': ")
if textFile == 'y' or textFile == 'Y':
importFile()
myMessage = input('Enter your text: ')
myKey = input('Enter your key: ')
myKey2 = input('Enter your second key: ')
if myMode == 'encrypt' or myMode == 'e':
translated = encryptMessage(myKey, myMessage)
elif myMode == 'decrypt' or myMode == 'd':
translated = decryptMessage(myKey, myMessage)
print('%sYour Message: ' % (myMode.title()))
print(translated)
print()
This is the Function:
def importFile():
importText = []
fileLocation = input('What is the name of the text file: ')
open('fileLocation''r')
As you can probably tell i am trying to import text files in to python but haven't passed the first hurdle:)
Here is all the code:
LETTERS = 'ZABCDEFGHIJKLMNOPQRSTUVWXY'
def main():
myMode = input("Encrypt 'e' or Decrypt 'd': ")
textFile = input("Would you like to import a text file 'Y' or 'N': ")
if textFile.lower() == 'y' :
importFile()
myMessage = input('Enter your text: ')
myKey = input('Enter your key: ')
myKey2 = input('Enter your second key: ')
if myMode == 'encrypt' or myMode == 'e':
translated = encryptMessage(myKey, myMessage)
elif myMode == 'decrypt' or myMode == 'd':
translated = decryptMessage(myKey, myMessage)
print('%sYour Message: ' % (myMode.title()))
print(translated)
print()
def encryptMessage(key, message):
return translateMessage(key, message, 'encrypt')
def decryptMessage(key, message):
return translateMessage(key, message, 'decrypt')
def translateMessage(key, message, mode):
translated = []
keyIndex = 0
keys = key.upper()
for symbol in message:
num =LETTERS .find(symbol.upper())
if num != -1:
if mode == 'encrypt':
num += LETTERS .find(key[keyIndex])
elif mode == 'decrypt':
num -= LETTERS .find(key[keyIndex])
num %= len(LETTERS)
if symbol.isupper():
translated.append(LETTERS[num])
elif symbol.islower():
translated.append(LETTERS[num].lower())
keyIndex += 1
if keyIndex == len(key):
keyIndex = 0
else:
translated.append(symbol)
return ''.join(translated)
if __name__ == '__main__':
main()
def importFile():
importText = []
fileLocation = input('What is the name of the text file: ')
open('fileLocation','r')
main()
It is the
if __name__ == '__main__':
main()
def importFile():
importText = []
fileLocation = input('What is the name of the text file: ')
open('fileLocation','r')
main()
part.
At
if __name__ == '__main__':
main()
the main()
function is called, but the importFile()
isn't defined yet. Move it above the mentionned lines.
The additional main()
at the end is not needed.