Sign in to follow this  
Followers 0
Embrisk

HELP PLEASE

5 posts in this topic

Posted (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 by Embrisk
0

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Yeah, I've tried StackOverflow. Nobody knows what's wrong.

0

Share this post


Link to post
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. 

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.