Python代码有几个函数,要求用户输入,输入后,代码结束,不执行

2024-09-30 20:22:59 发布

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

下面是我的代码。它将询问用户的输入。输入所需的输入并按enter键后,它将结束,而不会转到所需的函数执行。如果有人能帮我修一下,我将不胜感激

from statistics import mean
#headOfFile - prints the first 5 lines of the file passed in as a parameter
def headOfFile(myFile):
    return headOfFile
    f=open("textFile.txt", "r")
    for i in range(5):
        f.readline(5)
    print (i)
    f.close()
#tailOfFile - prints the last 5 lines of the file passed in as a paramete
def tailOfFile(myFile):
    return tailOfFile   
    f=open("textFile.txt","r")
    for i in range(5):
        f.readline(-5)
    print (i)
    
    f.close()
def wordCount(myFile):
    return wordCount
    f=open("textFile.txt", "rt")
    words = "textFile.txt".split()
    print ('Number of words in text file: ', len(words))
    file = open("textFile.txt", "r")
    lines = "textFile.txt"
    f.readline()
    print ("Number of lines in text file: ", len(lines))
    f.close()   
#stats - prints the smallest, largest, and average of the numbers in the file
def stats(myFile):
    return stats
    f=open("number.txt", "r")
    l=[ int(l) for l in input("number.txt").split(",")]
    minl = l[0]
    for i in range(len(l)):
        if l[i] < minl:
 minl = l[i]
    print ("The smallest number in the list is: ",minl)
    maxl = l[0]
    for i in range(len(l)):
        if l[i] > maxl:
            maxl = l[i]
    print ("The largest number in the list is: ", maxl)
    
    Average = sum(l) / float(len(l))
    my_list = ("number.txt")
    average =Average(my_list) 
    print ("Average of list is: ", average)
    f.close()
myFile = input("Enter the name of the first file you want to use(headOfFile, 
tailOfFile, wordCount, stats): ")
headOfFile(myFile)
tailOfFile(myFile)
wordCount(myFile)

Tags: oftheintxtnumberforlenopen
1条回答
网友
1楼 · 发布于 2024-09-30 20:22:59

您的函数都以return语句开头,出于某种原因,只返回对函数本身的引用。因此,函数不会做任何有用的事情,因为它们的其余代码都不会执行

相关问题 更多 >