Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-13-2010, 07:37 PM   PM User | #1
ThunderKatsHo
New to the CF scene

 
Join Date: Jun 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
ThunderKatsHo is an unknown quantity at this point
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
ThunderKatsHo is offline   Reply With Quote
Old 06-14-2010, 07:27 PM   PM User | #2
Samhain13
Regular Coder

 
Samhain13's Avatar
 
Join Date: Aug 2008
Location: Pilipinas
Posts: 165
Thanks: 4
Thanked 18 Times in 18 Posts
Samhain13 is on a distinguished road
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 **
Samhain13 is offline   Reply With Quote
Old 10-28-2010, 11:14 PM   PM User | #3
digitalmaniac
New Coder

 
Join Date: Oct 2010
Location: Bonifay, Fl
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
digitalmaniac is an unknown quantity at this point
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-28-2010 at 11:17 PM.. Reason: Didn't see the post above...
digitalmaniac is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:08 AM.


Advertisement
Log in to turn off these ads.