Global variable problem [SOLVED]



  • On 28/10/2014 at 10:57, xxxxxxxx wrote:

    Hi
    I'm trying to get global variables working - I have a strange problem where if make a comparison test - I can no longer change the value afterwards without crashing.

    for example...

    > import c4d
    >
    > from c4d import gui
    >
    > #Welcome to the world of Python
    >
    >
    >
    >
    > def main() :
    >
    >     if x != y:
    >
    >         y = 0
    >
    >         print y
    >
    >
    >
    >
    > if __name__=='__main__':
    >
    >     x = 3
    >
    >     y = 7
    >
    >     main()
    >
    >
    >
    gives the error,
    UnboundLocalError: local variable 'y' referenced before assignment



  • On 28/10/2014 at 12:39, xxxxxxxx wrote:

    Hi,

    an example.

      
    import c4d  
      
      
    def settings() :  
      global x,y  
      x=3  
      y=6  
      return x,y  
      
    def main() :  
      global x,y  
      settings()  
        
      if x != y:  
          y = 0  
          print y  
      
    if __name__=='__main__':  
      main()  
      
    

    Best wishes
    Martin



  • On 28/10/2014 at 12:56, xxxxxxxx wrote:

    Thanks for that Martin!



  • On 28/10/2014 at 13:24, xxxxxxxx wrote:

    you´re welcome

    another one:
    depends on what you´re going to do

      
      
    import c4d  
      
    global x,y  
    x=6  
    y=5  
      
      
    def main() :  
      global x,y  
        
        
      if x != y:  
          y = 0  
          print y  
      
    if __name__=='__main__':  
      main()  
      
    


  • On 28/10/2014 at 14:04, xxxxxxxx wrote:

    yeah that looks a bit simpler - cheers 🙂


Log in to reply