传递

时间:2017-10-05 21:57:51

标签: python variables optimization procedure

我想创建一个set_range过程,其目标是从3组中定义最大和最小的数字。最后一步是减去操作。

我写了前两部分,但最后一部分不起作用。问题似乎是将变量从一个传递给另一个......

#biggest number
def biggest(a, y, z):
    Max = a
    if y > Max:
        Max = y    
    if z > Max:
        Max = z
        if y > z:
            Max = y
    return Max
#print biggest(10, 4, 7) TEST ONLY

#smallest number
def smallest(a, y, z):
    Small = a
    if y < Small:
        Small = y    
    if z < Small:
        Small = z
        if y < z:
            Small = y
    return Small
#print smallest (10, 4, 7) TEST ONLY

#final part of the code, Max - Small operation
def set_range():
  m = Max
  s = Small

print set_range

1 个答案:

答案 0 :(得分:0)

通过这种方式,您可以访问代码中所有方法的所有变量,这意味着,从最大()和最小()分享数字

 class getOperationMax(a,y,z):

      def __init__(self,a,y,z):
        self.y = y
        self.a = y
        self.z = y
        self.Max = 0
        self.Small = 0

      #biggest number
      def biggest(self):
          self.Max = self.a
          if self.y > self.Max :
              self.Max = self.y
              self.max = self.y    
          if self.z > self.Max:
              self.Max = self.z
              if self.y > self.z:
                  self.Max = self.y

          return self.Max
      #print biggest(10, 4, 7) TEST ONLY

      #smallest number
      def smallest(self):
          self.Small = self.a
          if self.y < self.Small:
              self.Small = self.y    
          if self.z < self.Small:
              self.Small = self.z
              if self.y < self.z:
                  self.Small = self.y
          return self.Small
      #print smallest (10, 4, 7) TEST ONLY

    operation = getOperationMax(5,6,7)
    print operation.biggest()
    print operation.smallest()