python - Local Variable Reference Before Assignment -
ok i'm making program using tkinter (3.3). user submits name, name searched in database cards.cdb
prints out it's information , compares online source url
. displays picture of named thing using function picture
. when button clicked calls buttonclicked
function calls 2 other functions.
from tkinter import * import urllib import urllib.request bs4 import beautifulsoup import pydeck import sys collections import defaultdict root = tk() name="" def buttonclicked(): name() picture(text) def name(): all_lists=[] #all lists text = inputfield.get() pydeck.loaddatabase('c://users/trevor/desktop/y-in process/cards.cdb') cardname = pydeck.getcardsfromname(text) if not cardname == "": c = pydeck.card(cardname) tex.insert(end, c.name) level="\nlevel %s" % c.level + " " + c.attribute + " " + c.typestring tex.insert(end, level) atk="\natk: %s" % c.attack tex.insert(end, atk) defe="\ndef: %s" % c.defense tex.insert(end, defe) typestring='\n%s' %c.typestring desc='\n%s' %c.description seperator='\n--------------------\n' tex.insert(end, typestring) tex.insert(end, desc) tex.insert(end,seperator) #-- url_name=c.name.replace(' ','_') url=('http://yugioh.wikia.com/wiki/card_tips:{}'.format(url_name)) page = urllib.request.urlopen(url) soup = beautifulsoup(page.read()) content = soup.find('div',id='mw-content-text') links = content.findall('a') link_lists = defaultdict(list) all_lists.append([link.get("title") link in links]) common_links = set(all_lists[0]).intersection(*all_lists[1:]) omit_list=['none', 'special summon', 'edit searchable section', 'edit special summoned hand section','edit special summoned deck section','edit special summoned graveyard section','edit easier tribute summons section','edit generic tips section','edit traditional format section'] final=set(common_links)-set(omit_list) tex.insert(end,final) #-- tex.see(end) # scroll if necessary def picture(text): gifdir = "c:\\users\\trevor\\desktop\\y-in process\\pictures\\" pydeck.loaddatabase('c://users/trevor/desktop/y-in process/cards.cdb') cardname = pydeck.getcardsfromname(text) if not cardname == "": c=pydeck.card(cardname) filename='{}.gif' .format(c.cardid) img = photoimage(file=gifdir+filename) can = canvas(root) can.pack(fill=both,side='left') can.config(width=img.width(), height=img.height()) can.create_image(2, 2, image=img, anchor=nw) tex=text(root) tex.pack(side='right') inputfield = entry(root) inputfield.pack(side='bottom') = button(root,text="enter name", command = buttonclicked) #calls name function but.pack(side='bottom') text = inputfield.get() root.mainloop()
i'm getting error
filename='{}.gif' .format(c.cardid) unboundlocalerror: local variable 'c' referenced before assignment
i know means c
isn't being created in picture
function define doesn't recognize it.
anybody have suggestions cause i'm stumped?
if not cardname == "": c=pydeck.card(cardname) filename='{}.gif' .format(c.cardid)
and happens if cardname
is equal "" ?
since place c
initialized, if cardname
empty you'll see error. need define should happen cardname
empty, , handle condition accordingly.
Comments
Post a Comment