Python Forum
send function output from one py file to another (OOP)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
send function output from one py file to another (OOP)
#11
(Aug-25-2023, 01:46 PM)deanhystad Wrote: The quiz data should know nothing about the GUI. The GUI should know enough about the quiz data to use it.

For example, the GUI should ask quiz data for a question. Quiz data should return a question. GUI uses the returned question to update the panels.

As for your other questions, I have no clue. What is the quiz brain answer checker file? If it relates to quiz data, why isn't it part of the quiz brain? You talk like we're collaborating on this project, but all I've seen is one function. Please provide some context for your questions.
basically the data py file's function returns a list of dictionaries into the main py file
code for main file is as follows
data=Data()
ui=Gui()
question_data=data.choose_cat(ui.output_url())
print(question_data)
score=Score()
quiz_brain=QuizBrain(question_data)


then the quiz brain gets the list of dicitonaries and generates an example for which the code is

def generate_question(self):

self.q_number+=1
self.new_q=rand.choice(self.q_data)
question=html.unescape(self.new_q["question"])
answer=self.new_q["correct answer"]
self.q_text=f'Q{self.q_number}/50: {question}"
return self.q_text

in order to avoid a loop problem with the UI file (because the quizbrain functions output goes to UI in order to update the text on the canvas) i have since created a new answer checker file to receive the correct answer from the quiz brain file and the user answer from the UI file in order to compare the two answers. the code from the answer checker is as follows

def check_answer(self):
user_ans=Gui.get_ans()
cor_ans=QuizBrain.corr_answer()
if user_ans==cor_ans:
Gui.right()
else:
Gui.wrong()

the GUI wrong and right functions simply change the background color to the canvas widget in the UI file according to whether the answer was right or wrong
what i still need to figure out is how to generate a new question from the UI file when the appropriate function is taken as well as how to get the question data list of dictionaries assigned based on the UI optionmenu output
Larz60+ write Aug-25-2023, 05:35 PM:
Please don't forget to use BBCode tags
Reply
#12
Quote:basically the data py file's function returns a list of dictionaries into the main py file
Why doesn't the Quiz brain do this? The Quiz Brain is the thing that needs to know all the questions, right? Player selects category. GUI calls function in Quiz Brain to load questions for the selected category (passing the category as an argument). Quiz Brain calls function in Quiz Data to get the questions for the category.

Quote:in order to avoid a loop problem with the UI file (because the quizbrain functions output goes to UI in order to update the text on the canvas) i have since created a new answer checker file to receive the correct answer from the quiz brain file and the user answer from the UI file in order to compare the two answers
Good idea, bad implementation. The GUI should call this function and pass the anwer as an argument. The function should return if the answer is correct, and any information the GUI might want to display. This function should not know of the GUI's existence.

One more thing. You don't want to do this:
self.new_q=rand.choice(self.q_data)
To randomize the questions, use shuffle. Then you can pop() them one by one or iterate through the list. random.choice allows for asking the same question multiple times.
Reply
#13
(Aug-25-2023, 03:34 PM)deanhystad Wrote:
Quote:basically the data py file's function returns a list of dictionaries into the main py file
Why doesn't the Quiz brain do this? The Quiz Brain is the thing that needs to know all the questions, right? Player selects category. GUI calls function in Quiz Brain to load questions for the selected category (passing the category as an argument). Quiz Brain calls function in Quiz Data to get the questions for the category.

Quote:in order to avoid a loop problem with the UI file (because the quizbrain functions output goes to UI in order to update the text on the canvas) i have since created a new answer checker file to receive the correct answer from the quiz brain file and the user answer from the UI file in order to compare the two answers
Good idea, bad implementation. The GUI should call this function and pass the anwer as an argument. The function should return if the answer is correct, and any information the GUI might want to display. This function should not know of the GUI's existence.

One more thing. You don't want to do this:
self.new_q=rand.choice(self.q_data)
To randomize the questions, use shuffle. Then you can pop() them one by one or iterate through the list. random.choice allows for asking the same question multiple times.
Thanks for all your help so far. However my question still remains unanswered. How do i set up the function in the quiz brain file in order to get the list generated. And consequently how do i generate a new question? The way i have it setup at the moment is that the question gets generated upon initializaition however the question list will only get generated after the option menu option gets selected. So how do i set this up?
Reply
#14
Did you look at my example? The option menu control used to select the quiz category was bound to a function. Selecting a category calls the function. The function retrieved the question for the category and displayed the first question. Nothing is done at initialization because at initialization you don't know the category.

What control does the player use to select the quiz category in your program?
Reply
#15
(Aug-26-2023, 10:33 AM)deanhystad Wrote: Did you look at my example? The option menu control used to select the quiz category was bound to a function. Selecting a category calls the function. The function retrieved the question for the category and displayed the first question. Nothing is done at initialization because at initialization you don't know the category.

What control does the player use to select the quiz category in your program?

theres a select category button that triggers the selection
Reply
#16
The button callback/command should call the function that loads questions, passing the category as an argument. The button command/callback should then update the window to display the first question in the newly selected category and perform any operations required to start a quiz.

A good start is look for all the code you currently cannot perform at initialization. That is code you should execute when the button is pressed.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Not able to use the output from one function into another function AravindPandian 1 1,671 Oct-17-2019, 08:40 AM
Last Post: Evil_Patrick

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020