2013-08-11 5 views
6

मुझे कोशिश करने और कथन को छोड़कर कुछ समस्याएं हैं, मेरे पास एक एंट्री विजेट है जो तारों में इनपुट लेता है लेकिन मेरे पास कोड है जो इसे बाद में पूर्णांक में परिवर्तित करता है, समस्या है अगर पाठ की तरह उपयोगकर्ता इनपुट कुछ इसे इस तरह एक त्रुटि फेंकता है:असाइनमेंट से पहले स्थानीय चर संदर्भित किया गया है और कथन को छोड़कर

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "/usr/lib/python3.2/tkinter/__init__.py", line 1402, in __call__ 
    return self.func(*args) 
    File "/home/ppppwn3d/workspace/Python/JailBreakBob/JailBreakBob.py", line 157, in buttonclick_gamescreen 
    entryx = int(e1.get()) 
ValueError: invalid literal for int() with base 10: 'abc' 

तो मैं कोशिश के साथ और बयानों को छोड़कर त्रुटि को छिपाने के लिए चाहता था, लेकिन अब मैं एक और त्रुटि संदेश मिलता है।

यह कोड में जैसा दिखता है।

while pressed == 8 : 
     try: 
      entryx = int(e1.get()) 
      entryy = int(e2.get()) 
     except ValueError: 
      print("text") 

     answerx = answerlistx[randomimage] 
     answery = answerlisty[randomimage] 

     if entryx == answerx and entryy == answery 
      canvas.delete(images) 
      randomimage = random.randrange(0,49+1) 
      scorecounter = scorecounter + 1 
      game = PhotoImage(file=imagelist[randomimage]) 
      images = canvas.create_image(30, 65, image = game, anchor = NW) 
      e1.delete(0, END) 
      e2.delete(0, END) 
      pressed = '' 
     if entryx > 10 or entryx < -10 or entryy > 10 or entryy < -10 : 
      wrong = canvas.create_image(30, 65, image = outside, anchor = NW) 
      e1.delete(0, END) 
      e2.delete(0, END) 
      pressed = '' 
     else: 
      wrong = canvas.create_image(30, 65, image = incorrect, anchor = NW) 
      e1.delete(0, END) 
      e2.delete(0, END) 
      pressed = '' 

नया त्रुटि संदेश:

text 
Exception in Tkinter callback 
Traceback (most recent call last): 
    File "/usr/lib/python3.2/tkinter/__init__.py", line 1402, in __call__ 
    return self.func(*args) 
    File "/home/ppppwn3d/workspace/Python/JailBreakBob/JailBreakBob.py", line 165, in buttonclick_gamescreen 
    if entryx == answerx and entryy == answery: 
UnboundLocalError: local variable 'entryx' referenced before assignment 

मैं समझ नहीं क्यों यह हो रहा है और कैसे तय करने के लिए यह इतना किसी भी मदद की सराहना की जाएगी है।

अग्रिम धन्यवाद।

उत्तर

5

यदि आपके try/except ब्लॉक में कोई अपवाद है, तो entryx और entryy स्कोप में परिभाषित चर नहीं होंगे।

आपको या तो फेंकना और त्रुटि करना चाहिए या प्रोग्राम से बाहर निकलना चाहिए या entryx और entryyexcept ब्लॉक में डिफ़ॉल्ट मान असाइन करना चाहिए।

इसके अलावा, आप थोड़ी देर तक लूप बना सकते हैं जब तक कि उपयोगकर्ता एक पूर्णांक में प्रवेश न करे, जैसे here

0

आप 'टेक्स्ट' संदेश देख सकते हैं।
तो, अपवाद उठाया गया था और एंट्रीक्स प्रारंभ नहीं किया गया था।
है कि आप क्यों मिल गया है:

local variable 'entryx' referenced before assignment 
4

@alecxe's answer को जोड़ने के लिए, मैं नहीं बल्कि इसलिए की तरह कार्यक्रम को संशोधित करेगा:

entryx, entryy = 0, 0 
    try: 
     entryx = int(e1.get()) 
     entryy = int(e2.get()) 
    except ValueError: 
     print("text") 

असल में, क्या त्रुटि का अर्थ है कि एक त्रुटि के मामले में , entryx, entryy कभी भी कुछ भी असाइन नहीं किया जाता है, लेकिन फिर भी if..else चेक में संदर्भित किया जाता है।

try..except ब्लॉक के बाहर चर के लिए डिफ़ॉल्ट मान होना सर्वोत्तम है।

1

मैं ब्लॉक शुरू करने से पहले किसी के साथ ansx, answery शुरू करूंगा।

संबंधित मुद्दे

 संबंधित मुद्दे