我正在尝试创建一个程序,该程序询问文件名,打开文件,确定文件中的最大值和最小值,并计算文件中数字的平均值。我想打印最大值和最小值,并返回文件中值的平均数。该文件每行只有一个数字,从上到下由许多不同的数字组成。到目前为止,这是我的程序:
def summaryStats():
fileName = input("Enter the file name: ") #asking user for input of file
file = open(fileName)
highest = 1001
lowest = 0
sum = 0
for element in file:
if element.strip() > (lowest):
lowest = element
if element.strip() < (highest):
highest = element
sum += element
average = sum/(len(file))
print("the maximum number is ") + str(highest) + " ,and the minimum is " + str(lowest)
file.close()
return average
当我运行我的程序时,它给了我这个错误:
summaryStats()
Enter the file name: myFile.txt
Traceback (most recent call last):
File "/Applications/Wing101.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 1, in <module>
# Used internally for debug sandbox under external interpreter
File "/Applications/Wing101.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 8, in summaryStats
builtins.TypeError: unorderable types: str() > int()
我想我正在努力确定制作字符串的部分。你们有什么感想?