如何调用 python 中的函数?

How do I call a function in python?

我正在尝试调用一个名为 def importFile(): 的新函数,但我收到错误消息,指出 importFile() 未定义。我做错了什么,我知道这可能很简单,但我是编码新手。

这是我尝试调用函数的部分:

 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()

这是函数:

def importFile():
    importText = []
    fileLocation = input('What is the name of the text file: ')
    open('fileLocation''r')

你可能会说我正在尝试将文本文件导入 python 但还没有通过第一个障碍:)

这是全部代码:

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()

您确定在定义importFile 代码后调用main 吗?输入此代码时:

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()

def importFile():
    importText = []
    fileLocation = input('What is the name of the text file: ')
    open('fileLocation','r')

main()

它应该而且确实在我的机器上运行良好。

也许您的 importFile 函数在不同的文件中,在这种情况下您应该添加 from ****file**** import importFile.

我注意到的另一件事是你的最后一行,open('fileLocation','r'),需要一个逗号。

查看您的编辑后:

if __name__ == '__main__':
    main()

需要在定义importFile之后。

这是

if __name__ == '__main__':
    main()

def importFile():
    importText = []
    fileLocation = input('What is the name of the text file: ')
    open('fileLocation','r')

main()

部分。

if __name__ == '__main__':
    main()

调用了main()函数,但是importFile()还没有定义。将其移至上述行上方。

不需要在末尾添加 main()