2013-09-25 7 views
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) 

답변

3

개체가 없습니다. 함수는이고 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) 
+0

아 물론. 나는 그렇게 피곤하다 나는 그것을 볼 수 없었다 : P – user7628