python3中用户函数的问题没有定义

2024-04-20 03:50:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我想写一个程序来组织棒球运动员的数据和信息。我需要使用一个输入文件,其中包含所有的数据要组织,包括球员姓名,球队,命中记录等

我对我定义的一个函数有一个问题,我真的不知道该从这里走到哪里。每次我运行这个程序时,我总是在第73、25、53、49行得到一个错误——所有这些都与name "processFile" is not defined有关。我不确定是否应该在函数外将其定义为变量?你知道吗

我张贴的代码,我到目前为止,和数据文件,我组织。谢谢您!你知道吗

print("Welcome to The Baseball Database!")

def terminate():
    """This function quits the program"""
    userExit = input("Are you sure you want to quit? Yes or No?: ")
    userExit = userExit.lower()
    if userExit == "yes":
        print("The Baseball Database is terminating gracefully.")
        exit()
    if userExit == "no":
        introRerun()
    else:
        print("Please enter 'Yes' or 'No'.")
        terminate()

def assist():
    """This function offers help to the user"""
    print("The Baseball Database supports the following command functions:")
    print("INPUT - which allows you to input a file path")
    print("TEAM - which allows you to identify a team, and provide info about the players")
    print("REPORT - which reports the number of hits")
    print("HELP - for assistance")
    print("QUIT - to exit the database")
    print("Please choose from one of the above functions.")
    introRerun()


def filePath():
    """This function allows the user to input a file"""
    file = input("Please enter the file path to read: ")
    fileObject = open(file) 
    return fileObject


def introRerun():
    """This function reprompts the user to enter a command"""
    introLoop = input("Please enter a command, or type 'Help' for a list of options: ")
    introLoop = introLoop.lower()
    if introLoop == "quit":
        terminate()

    if introLoop == "help":
        assist()

    if introLoop == "input":
        filePath()

    if introLoop == "team":
        processFlie(fileObject)

    else:
        print("Please enter a valid command, for a list of options type 'Help'")
        introRerun()


def processFile(inputFilePath):
    """This function processes the input file"""
    myList = None
    fileHandle = open(inputFilePath, "r")
    for line in fileHandle:
        tokens = line.split(sep = ";")
        entry = {"Name" : tokens [0], "Team" : tokens [1], "Games Played" : tokens [2], "At Bats" : tokens [3], "Runs Scored" : tokens [4], "Hits" : tokens [5], "Doubles" : tokens [6], "Triples" : tokens [7], "Homeruns" : tokens [8]}
        myList.append(entry)
    return myList


playerList = None
#playerList = processFile(filePath)
intro = input("Please enter a command, or type 'Help' for a list of options: ")
intro = intro.lower()

if intro == "help":
    assist()
if intro == "input":
    filePath()
    introRerun()
if intro == "team":
    if playerList != None:
        print("Error! You must first INPUT a file to be read!")
        filePath()
    else:
        filePath()
if intro == "no" or intro == "quit":
    terminate()

输入:

De Aza, Alejandro; CWS; 153; 607; 84; 160; 27; 4; 17
Hunter, Torii; DET; 144; 606; 90; 184; 37; 5; 17
Hamilton, Josh; LAA; 151; 576; 73; 144; 32; 5; 21
Choo, Shin-Soo; CIN; 154; 569; 107; 162; 34; 2; 21
Upton, Justin; ATL; 149; 558; 94; 147; 27; 2; 27
Cabrera, Miguel; DET; 148; 555; 103; 193; 26; 1; 44
Posey, Buster; SF; 148; 520; 61; 153; 34; 1; 15
Suzuki, Ichiro; NYY; 150; 520; 57; 136; 15; 3; 7
Holliday, Matt; STL; 141; 520; 103; 156; 31; 1; 22
Headley, Chase; SD; 141; 520; 59; 130; 35; 2; 13
Cabrera, Asdrubal; CLE; 136; 508; 66; 123; 35; 2; 14
Pierzynski, A.J.; TEX; 134; 503; 48; 137; 24; 1; 17
Hoes, L.J.; HOU; 46; 167; 24; 48; 7; 2; 1
Young Jr., Eric; COL; 57; 165; 22; 40; 9; 3; 1
Hairston, Scott; CHC; 52; 99; 13; 17; 2; 0; 8
d'Arnaud, Travis; NYM; 31; 99; 4; 20; 3; 0; 1
Ankiel, Rick; NYM; 20; 66; 7; 12; 4; 1; 2
Ankiel, Rick; HOU; 25; 62; 6; 12; 3; 0; 5
den Dekker, Matt; NYM; 27; 58; 7; 12; 1; 0; 1
Sanchez, Angel; CWS; 1; 2; 0; 0; 0; 0; 0

Tags: thetoinputifdeffunctionthisfile
1条回答
网友
1楼 · 发布于 2024-04-20 03:50:29

您定义processFile,但使用processFlieil反转。。。只是错误的拼写错误。你知道吗

但代码中还有许多其他问题:

  • 您有一个命令loop,但只处理一次
  • 在整个程序中需要文件名,但不要将其保留在任何地方
  • 你试着打开。。。以前打开的结果!你知道吗
  • 如果你打开文件,就永远不会关闭它
  • 在调用append之前,将myList初始化为None(应该是:myList = []

祝你好运,一切修好后,它就会运行:-)

相关问题 更多 >