Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple python question.

    This seems kind of silly, but I must be missing something, as I cannot for the life of me get this counting button to work.

    It should work like this:

    1. Varible A is definied as 0

    2.The command 'count' is then executed when the button is pressed. Count Adds 1 to A, A is then printed, repeat step 2 for each button press.

    It seems so silly that I cant get this to work, it says A is yet to be defined, despite that being the third thing I do. Help would be appreciated, as I'm playing to do a calculator with this as more or less the base.

    http://pastebin.com/ZpzHpPm5

  • #2
    Regular Coder Samhain13's Avatar
    Join Date
    Aug 2008
    Location
    Pilipinas
    Posts
    169
    Thanks
    4
    Thanked 18 Times in 18 Posts
    I don't know why the pastebin code doesn't work, but this does:
    Code:
    a = 0
    
    def count():
        global a   # I just added this line.
        a += 1
        print a
    I am a Man of Truth. I am a Free Human Person. I am a Peacemaker.
    ** Independent Multimedia Artist in Pasig **

  • #3
    New Coder
    Join Date
    Oct 2010
    Location
    Bonifay, Fl
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Samhain13 View Post
    I don't know why the pastebin code doesn't work, but this does:
    Code:
    a = 0
    
    def count():
        global a   # I just added this line.
        a += 1
        print a
    Thats because if you want the varible to be in existence throughout the whole project you need to declare it as global. If not python will just overwrite it each time it sees that varible. Good practice is to put the "global a" at the very top of the project.
    Last edited by digitalmaniac; 10-29-2010 at 12:17 AM. Reason: Didn't see the post above...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •