A big challange!

python
tkinter

#1

so throughout my project i have had errors and i did find somway to fix them however i have recently deleted them and now i have lost it (100% file shredded delete so no data forensic software can help) and now i have just given up, i am looking for someone who can 1. add a retry function to my while loop 2. convert my code into easygui and 3. set the available classes to 1-3 and make it repeat the queston if the answer is <= 4 and 4. (optional) if anyone would be able to output the data from the file into a exel file in a folder on the computer of whoever runs the program that would be exellent (only a local file, 3 columbs for score, name and class). Although i would really appreciate help with this, i do realise that it's alot and if you need to be paid then give me a PM and we can discuss it - Ben

Earlier version of my code:

import random
name = input("Welcome to the arythmatic quiz, what is your name?")
Class = int(input("What class are you in, %s? " % (name)))
Questions_asked = 0
score = 0
def myFunc():
score = 0
if operators == 1:
answer = num + num2
question = int(input ("What is %s + %s? " % (num,num2)))
elif operators == 2:
answer = num - num2
question = int(input ("What is %s - %s? " % (num,num2)))
else:
answer = num * num2
question = int(input ("What is %s * %s? " % (num,num2)))

if answer == question:
    print("Well done!")
    score +=1
else:
    print("Sorry, that is incorrect!")
print("You achived %s/10!" % (score))

num = random.randint(1,10)
num2 = random.randint(1,10)
operators = random.randint(1,3)
while Questions_asked < 10:
num = random.randint(1,10)
num2 = random.randint(1,10)
operators = random.randint(1,3)
myFunc()
Questions_asked +=1
else:
print("Thankyou for participating in the arythmatic quiz, %s you achived %s/10!" % (name,score))


#2

@cloudjumper36761

Personally I would just use the tkinter module as it is a built-in and second if you OOP your code you don' have to have a loop for your questions the main-loop will keep your buttons on the screen.

Code:

__author__ = 'Charles Engen'

import tkinter as tk
from os.path import join, exists
from os import getcwd, makedirs
from random import randint, choice
from math import floor

path_history = join(getcwd(), "History")
path_old_problems = join(path_history, "Problems")

output_data = [
    "Welcome to the Arithmetic Test Machine!",
    "What is: %s %s %s = ",
    "Please Enter your answer[Round Down to a whole number]",
    "You answered Correctly",
    "You failed to correctly answer!",
    "Not implemented!"
]
random_problem = [
    "/",
    "+",
    "*",
    "-"
]


class MainGUI(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)
        self.current_difficulty = 5.0
        if exists(join(path_history, "game_data.txt")):
            self.history = dict()
            with open(join(path_history, "game_data.txt"), 'r') as file:
                for line in file:
                    self.history = eval(line.strip('\n'))
        else:
            self.history = {'correct': 0, 'wrong': 0}

        self.protocol('WM_DELETE_WINDOW', self.quit_game)

        self.current_answer = 0

        self.widgets = []

        self.display_area_text_var = tk.StringVar()
        self.question_field_var = tk.StringVar()

        self.main_file_menu = tk.Menu(self)
        self.config(menu=self.main_file_menu)

        self.resizable(width=False, height=False)
        self.geometry('{}x{}'.format(320, 180))

        self.first_file_menu = tk.Menu(self.main_file_menu, tearoff=0)
        self.sub_ffm_Start = tk.Menu(self.first_file_menu)
        self.main_file_menu.add_cascade(label="File", menu=self.sub_ffm_Start)

        self.sub_ffm_Start.add_separator()

        self.sub_ffm_Start.add_command(label="Start Game", command=self.start_game)

        self.sub_ffm_Start.add_separator()

        self.sub_ffm_Start.add_command(label="Convert History to CSV", command=self.convert_history_csv)

        self.sub_ffm_Start.add_separator()

        self.sub_ffm_Start.add_command(label="Quit Game", command=self.quit_game)

        self.display_area_text_var.set(output_data[0])

        self.display_area_text = tk.Label(self, textvariable=self.display_area_text_var)
        self.question_field = tk.Label(self, textvariable=self.question_field_var)

        self.cur_diff_var = tk.StringVar()
        self.cur_diff = tk.Label(self, textvariable=self.cur_diff_var)

        self.cur_diff_var.set("Current Difficulty: %s" % self.current_difficulty)

        self.cur_diff.grid(row=4, column=0, sticky='S')

        self.display_area_text.grid(row=0)
        self.question_field.grid(row=1, sticky='S')

        self.input_var = tk.StringVar()
        self.input_entry = tk.Entry(self, textvariable=self.input_var)
        self.input_entry.grid(row=2, sticky='S')

        self.input_entry.bind('<Return>', self.check_guess)

    def start_game(self):
        self.ask_question()

    def quit_game(self):
        self.save_history()
        self.quit()

    def ask_question(self):
        self.input_var.set("")
        self.display_area_text_var.set(output_data[2])
        # Sets Question field
        temp_1 = randint(1, int(self.current_difficulty**1.25))
        temp_2 = randint(1, int(self.current_difficulty**1.25))
        temp_choice = choice(random_problem)
        self.question_field_var.set(output_data[1] % (temp_1, temp_choice, temp_2))
        self.current_answer = floor(eval("%s%s%s" % (temp_1, temp_choice, temp_2)))
        self.save_problem("[%s, %s, %s]\n" % (temp_1, temp_choice, temp_2))
        self.display_area_text_var.set(output_data[2])

    def check_guess(self, *args):
        try:
            self.input_var.set(self.input_entry.get())
            if self.current_answer == int(self.input_var.get()):
                self.input_var.set(output_data[3])
                self.current_difficulty += 0.1
                self.history['correct'] += 1
                self.save_history()
            else:
                self.input_var.set(output_data[4])
                self.current_difficulty -= 0.1

                self.history['wrong'] += 1
            self.after(250, self.ask_question)
        except ValueError:
            pass
        self.cur_diff_var.set("Current Difficulty: %s, Max:%s"
                              % ("{0:.1f}".format(self.current_difficulty), int(self.current_difficulty**1.25)-1))

    def save_history(self):
        if exists(path_history):
            with open(join(path_history, "game_data.txt"), 'w+') as file:
                file.write(str(self.history))
        else:
            makedirs(path_history)
            self.save_history()

    def save_problem(self, data):
        if exists(path_old_problems):
            with open(join(path_old_problems, "problems.txt"), "a+") as file:
                file.write(str(data))
        else:
            makedirs(path_old_problems)
            self.save_problem(data)

    def convert_history_csv(self):
        self.display_area_text_var.set(output_data[5])
        pass



if __name__ == "__main__":
    root = MainGUI()
    root.mainloop()

#3

@cloudjumper36761

I updated the code a bit, should work better it also saves some files for the different stats and loads them once you start the game. Also, this was written in Python 3.5 but it should work in 2.7.* because I didn't use any version specific stuff but if it fails to work let me know.

__author__ = 'Charles Engen'

import tkinter as tk
from os.path import join, exists
from os import getcwd, makedirs
from random import randint, choice
from math import floor

path_history = join(getcwd(), "History")
path_old_problems = join(path_history, "Problems")

# This is all the output given to our player
output_data = [
    "Welcome to the Arithmetic Test Machine!",
    "What is: %s %s %s = ",
    "Please Enter your answer[Round Down to a whole number]",
    "You answered Correctly",
    "You failed to correctly answer!",
    "Not implemented!"
]

# These are the operators the game works with.
random_problem = [
    "/",
    "+",
    "*",
    "-"
]


class MainGUI(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)
        # Font Data
        self.font_data_display = ('times', 20)
        self.font_data_menu = ('times', 14)
        # This is the constant difficulty modifier
        self.__mod_diff = 1.35
        # This is the current Difficulty level
        if exists(join(path_history, "difficulty.dif")):
            with open(join(path_history, "difficulty.dif"), 'r') as file:
                file.seek(0)
                for line in file:
                    self.current_difficulty = eval(line.strip('\n'))
        else:
            self.current_difficulty = 5.0
        # This is the current Difficulty level
        if exists(join(path_history, "game_data.bin")):
            self.history = dict()
            with open(join(path_history, "game_data.bin"), 'rb') as file:
                file.seek(0)
                self.history = eval(file.read().decode('utf-8').strip('\n'))
        else:
            self.history = {'correct': 0, 'wrong': 0}

        # This overrides the default behavior of the 'x' out button
        self.protocol('WM_DELETE_WINDOW', self.quit_game)

        # This is the holder for the current answer
        self.__current_answer = 0

        # All the following are for the display
        self.display_area_text_var = tk.StringVar()
        self.question_field_var = tk.StringVar()

        self.main_file_menu = tk.Menu(self)
        self.config(menu=self.main_file_menu)

        self.resizable(width=False, height=False)
        self.geometry('{}x{}'.format(660, 220))

        # Drop-down menu
        self.first_file_menu = tk.Menu(self.main_file_menu, tearoff=0)
        self.sub_ffm_Start = tk.Menu(self.first_file_menu)
        self.main_file_menu.add_cascade(label="File", menu=self.sub_ffm_Start, font=self.font_data_menu)

        self.sub_ffm_Start.add_separator()

        self.sub_ffm_Start.add_command(label="Start Game", command=self.start_game, font=self.font_data_menu)

        self.sub_ffm_Start.add_separator()

        self.sub_ffm_Start.add_command(label="Convert History to CSV",
                                       command=self.convert_history_csv, font=self.font_data_menu)

        self.sub_ffm_Start.add_separator()

        self.sub_ffm_Start.add_command(label="Quit Game", command=self.quit_game, font=self.font_data_menu)
        # End Drop-down

        # Display Text
        self.display_area_text_var.set(output_data[0])

        self.display_area_text = tk.Label(self, textvariable=self.display_area_text_var, font=self.font_data_display)
        self.question_field = tk.Label(self, textvariable=self.question_field_var, font=self.font_data_display)

        self.cur_diff_var = tk.StringVar()
        self.cur_diff = tk.Label(self, textvariable=self.cur_diff_var, font=self.font_data_display)

        self.cur_diff_var.set("Current Difficulty: %s" % self.current_difficulty)

        self.cur_diff.grid(row=4, column=0, sticky='S')

        self.display_area_text.grid(row=0, columnspan=2, sticky='S')
        self.question_field.grid(row=1, columnspan=2, sticky='S')

        self.right_var = tk.StringVar()
        self.right = tk.Label(self, textvariable=self.right_var, font=self.font_data_display)

        self.wrong_var = tk.StringVar()
        self.wrong = tk.Label(self, textvariable=self.wrong_var, font=self.font_data_display)

        self.right.grid(row=5, column=0, sticky='ES')
        self.wrong.grid(row=5, column=1, sticky='WS')
        # End Display

        # Input Entry
        self.input_var = tk.StringVar()
        self.input_entry = tk.Entry(self, textvariable=self.input_var, font=self.font_data_display)
        self.input_entry.grid(row=2, columnspan=2, sticky='S')

        self.input_entry.bind('<Return>', self.check_guess)
        # End Input Entry

    def start_game(self):
        """
        This method starts the game
        """
        self.ask_question()

    def quit_game(self):
        """
        This method controls the quiting of the game.
        """
        try:
            self.save_history()
            self.save_difficulty()
            self.quit()
        except FileNotFoundError:
            self.quit()

    def ask_question(self):
        """
        This method has all the functionality of our question asking.
        """
        self.input_var.set("")
        self.display_area_text_var.set(output_data[2])
        # Sets Question field
        temp_1 = randint(1, int(self.current_difficulty**self.__mod_diff))
        temp_2 = randint(1, int(self.current_difficulty**self.__mod_diff))
        temp_choice = choice(random_problem)
        self.question_field_var.set(output_data[1] % (temp_1, temp_choice, temp_2))
        self.__current_answer = floor(eval("%s%s%s" % (temp_1, temp_choice, temp_2)))
        self.save_problem("[%s, '%s', %s]\n" % (temp_1, temp_choice, temp_2))
        self.display_area_text_var.set(output_data[2])

    def check_guess(self, *args):
        """
        This method checks the players guess.
        :param args: This argument is only to catch what tkinter passes to this method.
        """
        try:
            self.input_var.set(self.input_entry.get())
            if self.__current_answer == int(self.input_var.get()):
                self.input_var.set(output_data[3])
                self.current_difficulty += 0.1
                self.history['correct'] += 1
                self.save_history()
                self.save_difficulty()
            else:
                self.input_var.set(output_data[4])
                self.current_difficulty -= 0.1

                self.history['wrong'] += 1
            self.after(250, self.ask_question)
        except ValueError:
            pass
        self.cur_diff_var.set("Current Difficulty: %s, Max:%s"
                              % ("{0:.1f}".format(self.current_difficulty),
                                 int(self.current_difficulty**self.__mod_diff)-1))
        self.right_var.set("Correct: %s" % self.history['correct'])
        self.wrong_var.set("Wrong: %s" % self.history['wrong'])

    def save_history(self):
        """
        This method saves the game history.
        """
        if exists(path_history):
            with open(join(path_history, "game_data.bin"), 'wb') as file:
                file.write(str(self.history).encode('utf-8'))
        else:
            makedirs(path_history)
            self.save_history()

    def save_problem(self, data):
        """
        This method saves the problem as they are done.
        :param data: pass the problem to be saved, has to be in the format of
            [int(), operator, int()]
        """
        if exists(path_old_problems):
            with open(join(path_old_problems, "problems.txt"), "a+") as file:
                file.write(str(data))
        else:
            makedirs(path_old_problems)
            self.save_problem(data)

    def save_difficulty(self):
        """
        This method saves the game difficulty level.
        """
        if exists(path_history):
            with open(join(path_history, "difficulty.dif"), "w+") as file:
                file.write(str(self.current_difficulty))
        else:
            makedirs(path_history)
            self.save_difficulty()

    def convert_history_csv(self):
        """
        Yet to be implemented.
        """
        self.display_area_text_var.set(output_data[5])
        pass


if __name__ == "__main__":
    root = MainGUI()
    root.mainloop()