Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'assign': 0.07; 'method.': 0.07; 'puts': 0.07; 'advance': 0.07; 'string': 0.09; '(instead': 0.09; 'clause': 0.09; 'exception,': 0.09; 'exception.': 0.09; 'referenced': 0.09; 'try:': 0.09; 'valueerror:': 0.09; 'def': 0.12; '"0"': 0.16; '(without': 0.16; '.db': 0.16; 'addr': 0.16; 'corrects': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'inputs': 0.16; 'int)': 0.16; 'integer,': 0.16; 'letting': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'occurs.': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'remainder': 0.16; 'sqlalchemy': 0.16; 'stepping': 0.16; 'valueerror': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'value.': 0.19; 'input': 0.22; '(in': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'fine': 0.24; "haven't": 0.24; 'script': 0.25; 'help!': 0.26; 'shown': 0.26; '(for': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'chris': 0.29; 'field,': 0.30; 'code': 0.31; '"please': 0.31; 'figure': 0.32; 'another': 0.32; 'worked': 0.33; 'except': 0.35; 'problem.': 0.35; 'etc': 0.35; 'received:84': 0.35; 'but': 0.35; 'method': 0.36; 'thanks': 0.36; 'employee': 0.37; 'error.': 0.37; 'so,': 0.37; 'to:addr:python-list': 0.38; 'that,': 0.38; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'catch': 0.60; 'new': 0.61; 'simple': 0.61; 'show': 0.63; 'name': 0.63; 'such': 0.63; 'field': 0.63; 'choose': 0.64; 'box,': 0.64; 'zip': 0.64; 'occur': 0.65; 'phone': 0.66; 'city': 0.66; 'realized': 0.68; 'default': 0.69; 'age': 0.80; 'opens': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=A5niPsmG c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=mlFM_a_ONtUA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=WhezmC8C3ROoBbdtuQwA:9 a=pLGFOuxtpy3sIarH:21 a=2xcfLBBgmFCvVJnx:21 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 Date: Wed, 18 Mar 2015 03:57:12 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: UnboundLocalError in TKinter, SQLAlchemy script. References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1426651223 news.xs4all.nl 2895 [2001:888:2000:d::a6]:42317 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87666 On 2015-03-18 02:41, Chris Kavanagh wrote: > I have a simple script that takes user input (for an Employee) such as > name, age, etc then puts in an sqlite3 database. The script worked fine > until I realized one problem. The age input field is defined in > SQLAlchemy as an Integer, so if a user inputs a string instead of a > number in that field, an error will occur (in IDLE). . . > > So, I put a try: except clause in the code for the age field in the > add_data method. . . > try: > age = self.age_var.get() > except ValueError: > showinfo("Error:", "Please Enter A Number In Age Field.") > > If the user inputs a string (instead of an Int) the showinfo dialogue > box opens as it should, but when the user clicks "ok" in the box, I get > the error "UnboundLocalError: local variable 'age' referenced before > assignment". I tried using a default value above the try: except clause > (age=0), which corrects the error, but causes "0" to be saved in the > database instead of letting the user choose another value. I want to > figure why I get the error (with the try: except clause) and what > exactly to do about it. Thanks in advance for any help! > > Here's the original code (without the try: except clause above). . .Just > put the try: except clause in the age variable in the add_data method as > shown above to get the error. > [snip] > > def add_data(self): > name = self.name_var.get() > age = self.age_var.get() > addr = self.address_var.get() > city = self.city_var.get() > state = self.state_var.get() > zip = self.zip_var.get() > ssn = self.ssn_var.get() > phone = self.phone_var.get() > cell = self.cell_var.get() > # create new Employee in .db > new_person = Employee(name=name, age=age, address=addr, > city=city, state=state, zip=zip, ssn=ssn, phone=phone, cell=cell) > session.add(new_person) > session.commit() > session.close() > self.callback() > return > Try stepping through the method in your head. What happens if the user enters a non-number? A ValueError exception occurs. You catch that, show a dialog, but then continue on with the remainder of the method. Because of the exception, you haven't assigned to 'age', hence the UnboundLocalError exception. If you assign a default value to age, it'll continue on to where you put it into the database. Why not just return from the method after showing the dialog?