Embrisk he/him Posted June 2, 2019 Report Share Posted June 2, 2019 (edited) So, um, I may or may not have a python programming assignment due tomorrow. I was wondering if anyone could help me work out this code. (Please) It comes up with a name error and says that 'self' hasn't been defined. Anyone know why? (please help.....please) import tkinter import tkinter.messagebox import random import json class programGUI: def __init__(self): self.main=tkinter.Tk() self.main.title('Fruit Test') try: j=open('data.txt', 'r') self.data=json.load(j) j.close() except: tkinter.messagebox.showerror('File Error') self.main.destroy() return fib=0 for i in self.data: fib=fib+1 if fib<2: tkinter.messagebox.showerror('Not enough fruit') self.main.destroy() return self.Tbutton=tkinter.Button(text='True', command=lambda: checkAnswer(answer=True)) self.Fbutton=tkinter.Button(text='False', command=lambda: checkAnswer(answer=False)) self.Tbutton.pack(side='left') self.Fbutton.pack(side='right') def showQuestion(self): fruits=random.sample(self.data, 2) fruit0=fruits[0]['name'] fruit1=fruits[1]['name'] self.components=['calories', 'fibre', 'sugar', 'vitamin_c'] nutrition=random.choice(self.components) fruitNutr0=fruits[0][nutrition] fruitNutr1=fruits[1][nutrition] if nutrition=='vitamin_c': nutrition='vitamin c' measurement='milligrams' else: measurement='grams' moreLess=random.choice(['more', 'less']) self.mainMessage=tkinter.Label(self.main, text='100 '+str(measurement)+' of '+str(fruit0)+' contains '+moreLess+' '+str(nutrition)+' than a 100 '+str(measurement)+' of '+str(fruit1)) self.mainMessage.pack() def checkAnswer(answer=None): checkAnswer(showQuestion) if (answer==True and moreLess=='more' and fruitNutr0>fruitNutr1) or (answer==True and moreLess=='less' and fruitNutr0<fruitNutr1) or (answer==False and moreLess=='less' and fruitNutr0>fruitNutr1) or (answer==False and moreLess=='more' and fruitNutr0<fruitNutr1): tkinter.messagebox.showinfo('Correct!') else: tkinter.messagebox.showerror('Incorrect!') self=programGUI() showQuestion(self) self.showQuestion() <This is where it says I have a problem. tkinter.mainloop() gui=programGUI() Edit: The file I'm loading is a list containing dictionaries of fruit and nutritional information, eg: [{'name':'lemon', 'calories':'43', 'fibre':'75'...}{'name':'apple', 'calories':'89'....}] Edited June 2, 2019 by Embrisk 0 Quote Link to comment Share on other sites More sharing options...
+Invocation Posted June 2, 2019 Report Share Posted June 2, 2019 Try StackOverflow as well. I can't help with this since I don't have Python at the moment, but SO's site is full of people who'll help. 0 Quote Link to comment Share on other sites More sharing options...
Argent he/him Posted June 2, 2019 Report Share Posted June 2, 2019 You are really going to want to Google the exact error and/or search relevant threads on StackOverflow. Trust me when I say a big part of being a good software developer, professional or not, is being able to search for the problems you are having. 3 Quote Link to comment Share on other sites More sharing options...
Embrisk he/him Posted June 2, 2019 Author Report Share Posted June 2, 2019 Yeah, I've tried StackOverflow. Nobody knows what's wrong. 0 Quote Link to comment Share on other sites More sharing options...
Argent he/him Posted June 2, 2019 Report Share Posted June 2, 2019 20 minutes ago, Embrisk said: Yeah, I've tried StackOverflow. Nobody knows what's wrong. The biggest community of developers in the world is far more likely than this forum. I am not going to lock this or anything, just trying to manage expectations. 0 Quote Link to comment Share on other sites More sharing options...
platnumkid Posted October 5, 2019 Report Share Posted October 5, 2019 Sorry this is so delayed, I don't really check this part of the forum, and sorry if this is no longer interesting to you. I believe your problem is that the line your interpreter is complaining about is in the class scope, not inside any of the defined functions. This code gets run when the .py file (module) is loaded/launched. At that point there is no instance of the programGUI class and therefore nothing for self to point at and in fact no such thing as self has been defined. I'm not 100% on this as I'm mainly a C++ guy, but I'm fairly certain this is how the python interpreter works. 2 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.