简体   繁体   中英

Python variables passing

I want to create a set_range procedures whose goal is to define biggest and smallest number from group of 3. Final step would be minus operation.

I wrote the first two parts but final part is not working. The issue seems to be with passing the variables from one to another...

#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

This way you can access all variables from all methods within your code this means, share numbers from biggest() and smallest()

 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM