Question

I'm trying to get the maximum and minimum value from another object in the same file. But it keeps returning " object has no attribute 'max_value' ". Can someone tell me how to get the variables for every instance of the object? Here is the full code:

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)
Was it helpful?

Solution

You have no objects. You have only got functions, and the max_value name is a local variable.

You do return a tuple of both min_value and max_value, but you ignore that return. Use tuple assignment to capture both:

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top