Re: global variable not working inside function. Increment

2013-05-13 Thread charles benoit
On Friday, September 4, 2009 4:52:11 PM UTC-7, Rami Chowdhury wrote:
> > global no_picked
> > no_picked = 0
> >
> > def picked(object, event):
> >   no_picked += 1
> >   print no_picked
> 
> In order to be able to affect variables in the global scope, you need to  
> declare them global inside the function, and not at the global scope. So  
> your code should read:
> 
>   no_picked = 0
> 
>   def picked(object, event):
>   global no_picked
>   no_picked += 1
>   print no_picked
> 
> I believe that will work.
> 
> On Fri, 04 Sep 2009 16:43:27 -0700, Helvin  wrote:
> 
> > Hi,
> >
> > This increment thing is driving me nearly to the nuts-stage. > <
> >
> > I have a function that allows me to pick points. I want to count the
> > number of times I have picked points.
> >
> > global no_picked
> > no_picked = 0
> >
> > def picked(object, event):
> >   no_picked += 1
> >   print no_picked
> >
> > Error msg says: UnboundLocalError: local variable 'no_picked'
> > referenced before assignment
> > For some reason, no_picked does not increment, but the printing
> > statement works.
> >
> > Do you know why?
> >
> > (I'm actually writing this for a vtkrenderwindowinteractor.)
> >
> > Helvin
> 
> 
> 
> -- 
> Rami Chowdhury
> "Never attribute to malice that which can be attributed to stupidity" --  
> Hanlon's Razor
> 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)

-- 
http://mail.python.org/mailman/listinfo/python-list


python adds an extra half space when reading from a string or list

2013-06-28 Thread charles benoit
number_drawn=()
def load(lot_number,number_drawn):
first=input("enter first lot: ")
last=input("enter last lot: ")
for lot_number in range(first,last):
line_out=str(lot_number)
for count in range(1,5):
number_drawn=raw_input("number: ")
line_out=line_out+(number_drawn)
print line_out
finale_line.append(line_out)
finale_line2=finale_line

load(lot_number,number_drawn)


print finale_line
print(" "*4),
for n in range(1,41):
print n,  #this is to produce a line of numbers to compare to
output#
for a in finale_line:
print"\n",
print a[0]," ",
space_count=1
for b in range(1,5):
if int(a[b])<10:
 print(" "*(int(a[b])-space_count)),int(a[b]),
 space_count=int(a[b])
else:
print(" "*(a[b]-space_count)),a[b],
space_count=a[b]+1







number_drawn=()
def load(lot_number,number_drawn):
first=input("enter first lot: ")
last=input("enter last lot: ")
for lot_number in range(first,last):
line_out=str(lot_number)
for count in range(1,5):
number_drawn=raw_input("number: ")
line_out=line_out+(number_drawn)
print line_out
finale_line.append(line_out)
finale_line2=finale_line

load(lot_number,number_drawn)


print finale_line
print(" "*4),
for n in range(1,41):
print n,  #this is to produce a line of numbers to compare to
output#
for a in finale_line:
print"\n",
print a[0]," ",
space_count=1
for b in range(1,5):
if int(a[b])<10:
 print(" "*(int(a[b])-space_count)),int(a[b]),
 space_count=int(a[b])
else:
print(" "*(a[b]-space_count)),a[b],
space_count=a[b]+1








this generates

enter first lot: 1
enter last lot: 4
number: 2
number: 3
number: 4
number: 5
12345
number: 1
number: 2
number: 3
number: 4
21234
number: 3
number: 4
number: 5
number: 6
33456
['12345', '21234', '33456']
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40
1 2   3   4   5
21   2   3   4
3  3   4   5   6
>#as you can see many numbers are between the lines of a normal print#
#I thought this was due to "white space" int he format .So I tried  a list
of strings and got the same results.#
-- 
http://mail.python.org/mailman/listinfo/python-list