473,320 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Why? UnboundLocalError: local variable 'red' referenced before assignment

I don't know ,why it's error
This my code
Expand|Select|Wrap|Line Numbers
  1. from PIL import Image
  2.  
  3. def ch_red(tmp):
  4.  
  5.     R = tmp[0]
  6.     G = tmp[1]
  7.     B = tmp[2]
  8.  
  9.     if 60 <= R <= 99 and G <= 50 and B <= 50:
  10.         red = red + 1
  11.  
  12.     elif 100 <= R <= 139 and G <= 100 and B <= 150 :
  13.         red = red + 1
  14.  
  15.     elif 140 <= R <= 229 and G <= 160 and B <= 225 :
  16.         red = red + 1
  17.  
  18.     elif 230 <= R <= 249 and G <= 210 and B <= 210 :
  19.         red = red + 1  
  20.  
  21.     elif 250 <= R <= 255 and G <= 240 and B <= 240 :
  22.         red = red + 1  
  23.  
  24.     return red
  25.  
  26. def ck_color(s):
  27.  
  28.     for i in range(0,s.size[0]):
  29.  
  30.         for j in range(0,s.size[1]):
  31.  
  32.             tmp = s.getpixel((i,j))
  33.  
  34.             red = ch_red(tmp)
  35.  
  36.     return red
  37.  
  38. p = Image.open("c:\pypro\color\crop(150_100_270_130)\111854\111854(1).jpg")
  39.  
  40. m = ck_color(p)
  41.  
  42. print m
  43.  
when I run .It show

UnboundLocalError: local variable 'red' referenced before assignment

Then I fx them to add

'global red '

in ch_red(tmp):
But it show error again

NameError: global name 'red' is not defined

I don't know, How I fix it?
Please.
Thank You.
Nae
Jan 20 '09 #1
1 6527
bvdet
2,851 Expert Mod 2GB
Variable red is not defined until this statement:
Expand|Select|Wrap|Line Numbers
  1. red = ch_red(tmp)
Variable red is also local to functions ch_red() and ck_color(). The script will fail even if you declare red to be global in both functions. Can you tell us what you are trying to do?

-BV
Jan 20 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Brad Clements | last post by:
I was going to file this as a bug in the tracker, but maybe it's not really a bug. Poor Python code does what I consider to be unexpected. What's your opinion? With Python 2.3.2 (but also...
2
by: silverburgh.meryl | last post by:
Can you please tell me what is the meaning this error in general? UnboundLocalError: local variable 'colorIndex' referenced before assignment In my python script, I have a variable define and...
15
by: Paddy | last post by:
Hi, I am trying to work out why I get UnboundLocalError when accessing an int from a function where the int is at the global scope, without explicitly declaring it as global but not when accessing...
9
by: Camellia | last post by:
hi all why it generates an "UnboundLocalError" when I do the following: <code> .... def main(): number = number() number_user = user_guess() while number_user != number:
2
by: Matthew Franz | last post by:
I'm probably fundamentally misunderstanding the way the interpreter works with regard to scope, but is this the intended behavior... franz-macbook:~ mdfranz$ python unboundlocal.py ('Darwin',...
2
by: Wang, Harry | last post by:
$$ TestCase ID : 001 Step : deleteDvc,206268 Result Eval type : XmlChk Step : deleteDvc,206269 Result Eval type : XmlChk Traceback (most recent call last): File "C:\UDR2\UDRxmlGateway.py", line...
0
by: Terry Reedy | last post by:
Mr SZ wrote: you need global ORIG_TYPE,ORIG_MSG because this otherwise marks them as local.
8
by: defn noob | last post by:
isPrime works when just calling a nbr but not when iterating on a list, why? adding x=1 makes it work though but why do I have to add it? Is there a cleaner way to do it? def isPrime(nbr):...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.