简体   繁体   English

Python变量传递

[英]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. 我想创建一个set_range过程,其目标是定义3组中的最大和最小数目。最后一步是减号运算。

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() 这样,您可以从代码中的所有方法访问所有变量,这意味着共享来自maximum()和Minimum()的数字

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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