我正在尝试从同一文件中的另一个对象获取最大值和最小值。但它一直返回“对象没有属性'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)