Jump to content

HELP PLEASE


Embrisk

Recommended Posts

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 by Embrisk
Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

  • 4 months later...

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...