【问题标题】:save variables on a while loop在while循环中保存变量
【发布时间】:2014-09-23 08:36:24
【问题描述】:

我是 python 新手。这是我想在我的代码中做的一个小例子。我想保存正方形和圆形的值。 所以它会问“确实想改变值......”你按一个正方形,它从 0 到 1,然后它再次询问,你按 2,然后它再次上升到 2。 我不想在我的程序中使用全局变量。 我正在阅读帖子说无全局变量的答案是将变量传递给函数并进行更改然后返回它们。我认为这不适用于我的 while 循环。

loopcounter = 0
square = 0
circle = 0

 def varibleChanges(circle, square):
    #global circle, square
    print 'Would you like to change circle or square?'
    print '1. circle' '\n' '2. square'
    choice = raw_input('>>')
    if choice == '1':
        square = square + 1
    elif choice == '2':
    circle = circle + 1
    print 'square: ', square 
    print 'circle: ', circle

while loopcounter <=2:
    print 'this is the begining of the loop'
    varibleChanges(circle, square)
    loopcounter +=1
    print "this is the end of the loop\n"

将变量存储在代码之外工作,例如写入文件,(无论如何我都会有保存功能) 还是最好重新考虑一下代码?

【问题讨论】:

    标签: python variables while-loop


    【解决方案1】:

    虽然对于您这样规模的程序来说不是必需的,但您可以考虑使用类。

    class Circle(object):
        def __init__(self, value):
            self.value = value
        def getValue(self):
            return self.value
        def incValue(self, add):
            self.value += add
    
    circle = Circle(0) #Create circle object
    circle.incValue(1)
    print(circle.getValue())
    

    当您处理较大的程序时,类会更有用。例如,如果您有多个圆,您将从这个圆类创建许多圆对象。然后,您可以分别处理每个圈子。

    你现在可能最好选择一个更简单的答案,但你最终肯定会使用类。

    请参阅here 了解 Python 中的类。

    【讨论】:

      【解决方案2】:

      返回变量,然后将它们传回,就可以很好地处理您的代码。如果您将代码修改为以下内容:

      def varibleChanges(circle, square):
          #skip to the end..
          return circle, square
      
      while loopcounter <=2:
          print 'this is the begining of the loop'
          circle, square = varibleChanges(circle, square)
          loopcounter +=1
          print "this is the end of the loop\n"
      

      然后你应该看到你想要的行为。

      作为旁注,您可以编写如下内容:

      circle = circle + 1
      

      作为

      circle += 1
      

      在 Python 中。快乐编码!

      【讨论】:

      • 哇,它有效!所以我猜只有在循环的第一级保存的变量被改变了,其他的都被重置了。
      • 是的,从某种意义上说。要阅读的概念是“范围界定”。函数内部的变量不能在函数外部看到(它们的范围受到限制),除非您将它们传递出函数(或者它们通过引用传递)。我建议您阅读 Dive Into Python (diveintopython.net)。它将涵盖有关范围如何工作、通过引用或值传递哪些 python 类型以及其他重要概念的主题。
      【解决方案3】:

      如果variableChanges 返回一个元组:它要修改的形状的名称和新值,那么shapes 不需要是全局的,甚至可以在 variableChanges 中使用。

       def variableChanges(circle, square):
          print 'Would you like to change circle or square?'
          print '1. circle' '\n' '2. square'
          choice = raw_input('>>')
          if choice == '1':
              return ('square', square + 1)
          elif choice == '2':
              return ('circle', circle + 1)
      
      loopcounter = 0
      shapes = {
          'square' = 0,
          'circle' = 0
      }
      
      while loopcounter <= 2:
          print 'this is the begining of the loop'
          shape, value = variableChanges(shapes['circle'], shapes['square'])
          shapes[shape] = value
          print (shape, value)
          loopcounter += 1
          print "this is the end of the loop\n"
      

      【讨论】: