Chambers
-- -- --

Mathematic addition game

Anonymous in /c/coding_help

115
Hi everyone. I am a beginner in python. I've been working on a simple math game app for about three days now. It's almost complete. But I want to add one more featutre. After the game is over, I want the game to ask the player if they want to play again. If they click yes, the same window closes and a new window opens to start a new game. If they click no, I want the app to close. Please help me how I can implment it.<br><br>```python<br>from tkinter import *<br>from random import randint, choice<br><br># Create the window<br>root = Tk()<br>root.title("Math addition game")<br><br># Create the widgets<br>point = Label(root, text="Points: 0", font=("Arial", 20))<br>point.pack()<br><br>question = Label(root, text="", font=("Arial", 20))<br>question.pack()<br><br>answer = Entry(root, font=("Arial", 20), width=10)<br>answer.pack()<br><br>correctness = Label(root, text="", font=("Arial", 20))<br>correctness.pack()<br><br>submit = Button(root, text="Submit", font=("Arial", 20), command=lambda: check_answer(answer.get(), points))<br>submit.pack()<br><br>replay = Button(root, text="Replay", font=("Arial", 20))<br>replay.pack()<br><br># Initialize points variable<br>points = 0<br><br>def set_question():<br> global points<br> global answer<br> global question<br> addition = [randint(1, 100) for _ in range(2)]<br> correct_answer = addition[0] + addition[1]<br> question.config(text=f"What is {addition[0]} + {addition[1]}")<br><br>def check_answer(answer, points):<br> global correctness, question, point<br> if answer == "":<br> correctness.config(text="Please enter an answer")<br> addition = [randint(1, 100) for _ in range(2)]<br> question.config(text=f"What is {addition[0]} + {addition[1]}")<br> return<br> if int(answer) == addition[0] + addition[1]:<br> points += 1<br> correctness.config(text="Correct answer")<br> point.config(text=f"Points: {points}")<br> else:<br> correctness.config(text="Wrong answer")<br> point.config(text=f"Points: {points}")<br> set_question()<br> answer.delete(0, END)<br><br>def replay_game():<br> global points, root<br> root.destroy()<br> root = Tk()<br> root.title("Math addition game")<br> set_question()<br> check_answer(0, 0)<br><br>set_question()<br>replay.config(command=replay_game)<br><br>root.mainloop()<br>```<br><br>I'm using tkinter as GUI library. I added two buttons submit and replay. Submit will check the answer and replay will start a new game. The addition variables are global variables. I want to access them in these two functions. <br><br>The problem is the check_answer function is being called before the replay_game function. The check_answer function requires the answer addition variable, but this variable is changed after the replay_game function is called. So, the addition variable for the check_answer function is not the same as the addition addition variable for the replay_game function. So, the addition question for the new game window is different than the addition for the check_answer function. <br><br>So, how can I access the addition variable in the check_answer function without the addition variable being changed?<br><br>I hope this makes sense. Please let me know if you need any more info.<br><br>Thanks in advance.<br><br>P.S. Please do not suggest me to use another GUI library.

Comments (3) 6197 👁️