0

我正在尝试从同一文件中的另一个对象获取最大值和最小值。但它一直返回“对象没有属性'max_value'”。有人可以告诉我如何获取对象的每个实例的变量吗?这是完整的代码:

from math import *
from graphics import *


def compute_min_and_max(expression):

    #Various constants
    print("Evaluating ", expression)
    min_value = 1e200
    max_value = -1e200


    #Gets maximum and minimum for x between 0 and 10
    for i in range(0, 1001):

        try:
            x = i/100.0
            y = eval(expression)
            min_value = min(y, min_value)
            max_value = max(y, max_value)

        except Exception:
            print("For ", x, " the expression is invalid")
            pass


    print("Your min and max numbers are *drum roll*...")
    return(min_value, max_value)


def compute_min_and_max_for_file(filename):

    global_min = 1e200
    global_max = -1e200
    opened_function_file = open(filename, 'r')

    #Gets global max and min
    for line in opened_function_file:
        compute_min_and_max(line)
        global_max = max(compute_min_and_max(line).max_value, global_max)
        global_min = min(compute_min_and_max(line).min_value, global_min)


    #Closes file, and returns the values
    opened_function_file.close()
    return (global_min, global_max)
4

1 回答 1

3

你没有对象。您只有functions,并且max_value名称是局部变量。

您确实返回了一个 和 的元组min_valuemax_value但您忽略了该返回。使用元组赋值来捕获两者:

for line in opened_function_file:
    min_value, max_value = compute_min_and_max(line)
    global_max = max(max_value, global_max)
    global_min = min(min_value, global_min)
于 2013-09-25T19:39:40.990 回答