簡體   English   中英

無論我把“score = 0”放在哪里,它都會忽略它,並出現一個關於賦值前使用的局部變量的錯誤

[英]No matter where I put "score = 0" It ignores it and comes up with an error about local variable used before assignment

在 python 調試器上,首先讀取分數,我嘗試將其全球化以查看是否有任何工作。 我也知道代碼很亂,有更好的方法來做,但我只想先解決這個問題。 這是我第一次使用堆棧溢出,所以如果您想運行代碼,我不知道如何將 zip 文件夾鏈接到問題。 這些文件夾包含命名為數組中內容的 PNG 圖像。 那位工作沒有任何問題。 這也是我第一次使用 tkinter。

global score
score = 0
from tkinter import *
from PIL import ImageTk, Image
from random import randint
import os



root = Tk()
root.title("Flashcard")
#root.iconbitmap('c:/')
root.geometry("500x500")


global dir_path
dir_path = os.path.dirname(os.path.realpath(__file__))

def score0():
    score = 0

def scoreadd():
    score = score+1

def scoreminus():
    score = score-1




def checkcorrectcs(comp_questions, random_number,):
    answer = answer_input.get()
    answer = answer.replace(" ", "")

    if answer.lower() == comp_questions[random_number]:
        checked = "Correct"
        scoreadd()

    else:
        checked = "Incorrect it was " + comp_questions[random_number].title()
        scoreminus()
    answer_label.config(text=checked)
    

def checkcorrectmaths(maths, random_number2,):
    answer = answer_input.get()
    answer = answer.replace(" ", "")
    if answer.lower() == math_questions[random_number2]:
        checked = "Correct"
        scoreadd()
    else:
        checked = "Incorrect it was " + math_questions[random_number2].title()
        scoreminus()
    answer_label.config(text=checked)

def checkcorrectph(physics_questions, random_number3,):
    answer = answer_input.get()
    answer = answer.replace(" ", "")
    if answer.lower() == physics_questions[random_number3]:
        checked = "Correct"
        scoreadd()
    else:
        checked = "Incorrect it was " + physics_questions[random_number3].title()
        scoreminus()
    answer_label.config(text=checked)

def checkcorrectbio(biology_questions, random_number4,):
    answer = answer_input.get()
    answer = answer.replace(" ", "")
    if answer.lower() == biology_questions[random_number4]:
        checked = "Correct" 
        scoreadd()
    else:
        checked = "Incorrect it was " + biology_questions[random_number4].title()
        scoreminus()
    answer_label.config(text=checked)

def checkcorrectchem(chemistry_questions, random_number5,):
    answer = answer_input.get()
    answer = answer.replace(" ", "")
    if answer.lower() == chemistry_questions[random_number5]:
        checked = "Correct"
        scoreadd()
    else:
        checked = "Incorrect it was " + chemistry_questions[random_number5].title()
        scoreminus()
    answer_label.config(text=checked)













#Computer science function
def computer_science():

    hide_any_windows()
    computer_science_window.pack(fill="both",expand=1)
    title = Label(computer_science_window, text="Computer science").pack()
    
    comp_questions = ["and", "binary", "denary", "hexadecimal", "or"]
    random_number = randint(0, len(comp_questions)-1)
    random_comp_question = f"{dir_path}/ComputerScienceQuestionBank/" + comp_questions[random_number] +".png"

    global comp_question_photo
    comp_question_photo = ImageTk.PhotoImage(Image.open(random_comp_question))
    show_comp_question = Label(computer_science_window, image=comp_question_photo)
    show_comp_question.pack(pady=15)

    #answer box
    global answer_input
    answer_input = Entry(computer_science_window, font=("Comic Sans", 20))
    answer_input.pack(pady = 15)

    confirm_button = Button(computer_science_window, text ="Confirm", command=lambda: checkcorrectcs(comp_questions, random_number))
    confirm_button.pack(pady=5)

    random_button = Button(computer_science_window, text= "New question", command=computer_science)
    random_button.pack(pady=10)

    

    global answer_label
    answer_label = Label(computer_science_window, text="")
    answer_label.pack(pady=15)  


#Maths function
def maths():
    hide_any_windows()
    maths_window.pack(fill="both",expand=1)
    title = Label(maths_window, text="Maths").pack()

    math_questions = ["144", "test2"]
    random_number2 = randint(0, len(math_questions)-1)
    random_math_question = f"{dir_path}/MathQuestionBank/" + math_questions[random_number2] +".png"

    global math_question_photo
    math_question_photo = ImageTk.PhotoImage(Image.open(random_math_question))
    show_math_question = Label(maths_window, image=math_question_photo)
    show_math_question.pack(pady=15)

    #answer box
    global answer_input
    answer_input = Entry(maths_window, font=("Comic Sans", 20))
    answer_input.pack(pady = 15)

    confirm_button = Button(maths_window, text ="Confirm", command=lambda: checkcorrectcs(math_questions, random_number2))
    confirm_button.pack(pady=5)

    random_button = Button(maths_window, text= "New question", command=maths)
    random_button.pack(pady=10)

    

    global answer_label
    answer_label = Label(maths_window, text="")
    answer_label.pack(pady=15)  



#Physics function
def physics():
    hide_any_windows()
    physics_window.pack(fill="both",expand=1)
    title = Label(physics_window, text="Maths").pack()

    physics_questions = ["9.81", "test3", "quarks", "speedoflight"]
    random_number3 = randint(0, len(physics_questions)-1)
    random_physics_question = f"{dir_path}/PhysicsQuestionBank/" + physics_questions[random_number3] +".png"

    global physics_question_photo
    physics_question_photo = ImageTk.PhotoImage(Image.open(random_physics_question))
    show_physics_question = Label(physics_window, image=physics_question_photo)
    show_physics_question.pack(pady=15)

    #answer box
    global answer_input
    answer_input = Entry(physics_window, font=("Comic Sans", 20))
    answer_input.pack(pady = 15)

    confirm_button = Button(physics_window, text ="Confirm", command=lambda: checkcorrectph(physics_questions, random_number3))
    confirm_button.pack(pady=5)

    random_button = Button(physics_window, text= "New question", command=physics)
    random_button.pack(pady=10)

    

    global answer_label
    answer_label = Label(physics_window, text="")
    answer_label.pack(pady=15)



#Biology function
def biology():
    hide_any_windows()
    biology_window.pack(fill="both",expand=1)
    title = Label(biology_window, text="Biology").pack()

    biology_questions = ["test3", "test4"]
    random_number4 = randint(0, len(biology_questions)-1)
    random_biology_question = f"{dir_path}/BiologyQuestionBank/" + biology_questions[random_number4] +".png"

    global biology_question_photo
    biology_question_photo = ImageTk.PhotoImage(Image.open(random_biology_question))
    show_biology_question = Label(biology_window, image=biology_question_photo)
    show_biology_question.pack(pady=15)

    #answer box
    global answer_input
    answer_input = Entry(biology_window, font=("Comic Sans", 20))
    answer_input.pack(pady = 15)

    confirm_button = Button(biology_window, text ="Confirm", command=lambda: checkcorrectbio(biology_questions, random_number4))
    confirm_button.pack(pady=5)

    random_button = Button(biology_window, text= "New question", command=biology)
    random_button.pack(pady=10)

    

    global answer_label
    answer_label = Label(biology_window, text="")
    answer_label.pack(pady=15)



#Chemistry function
def chemistry():
    hide_any_windows()
    chemistry_window.pack(fill="both",expand=1)
    title = Label(chemistry_window, text="Chemistry").pack()

    chemistry_questions = ["jameschadwick", "loweractivationenergy", "mendeleev", "postive", "protondonors",]
    random_number5 = randint(0, len(chemistry_questions)-1)
    random_chemistry_question = f"{dir_path}/ChemistryQuestionBank/" + chemistry_questions[random_number5] +".png"

    global chemistry_question_photo
    chemistry_question_photo = ImageTk.PhotoImage(Image.open(random_chemistry_question))
    show_chemistry_question = Label(chemistry_window, image=chemistry_question_photo)
    show_chemistry_question.pack(pady=15)

    #answer box
    global answer_input
    answer_input = Entry(chemistry_window, font=("Comic Sans", 20))
    answer_input.pack(pady = 15)

    confirm_button = Button(chemistry_window, text ="Confirm", command=lambda: checkcorrectchem(chemistry_questions, random_number5))
    confirm_button.pack(pady=5)

    random_button = Button(chemistry_window, text= "New question", command=chemistry)
    random_button.pack(pady=10)

    

    global answer_label
    answer_label = Label(chemistry_window, text="")
    answer_label.pack(pady=15)













def hide_any_windows():
    for widget in computer_science_window.winfo_children():
        widget.destroy()

    for widget in maths_window.winfo_children():
        widget.destroy()

    for widget in physics_window.winfo_children():
        widget.destroy()

    for widget in biology_window.winfo_children():
        widget.destroy()

    for widget in chemistry_window.winfo_children():
        widget.destroy()


    computer_science_window.pack_forget()
    maths_window.pack_forget()
    physics_window.pack_forget()
    biology_window.pack_forget()
    chemistry_window.pack_forget()


#menu
my_menu = Menu(root)
root.config(menu=my_menu)




#Subjects for the menu
subjects_menu = Menu(my_menu)
my_menu.add_cascade(label = "Subjects", menu=subjects_menu)
subjects_menu.add_command(label="Computer science", command=computer_science)
subjects_menu.add_command(label="Maths", command=maths)
subjects_menu.add_command(label="Physics", command=physics)
subjects_menu.add_command(label="Biology", command=biology)
subjects_menu.add_command(label="Chemistry", command=chemistry)
subjects_menu.add_separator()
subjects_menu.add_command(label="Exit", command=root.quit)



#Making the window
computer_science_window = Frame(root, width=500, height=500)
maths_window = Frame(root, width=500, height=500)
physics_window = Frame(root, width=500, height=500)
biology_window = Frame(root, width=500, height=500)
chemistry_window = Frame(root, width=500, height=500)

# all_windows = [computer_science_window, maths_window, physics_window, biology_window, chemistry_window]



        
root.mainloop()

您可以在修改它的每個函數中為變量score添加一個全局聲明。 (簡單方便,但不推薦)

score = 0

def score0():
    global score
    score = 0

def scoreadd():
    global score
    score = score + 1

def scoreminus():
    global score
    score = score - 1

或者創建一個小的Score class來記錄分數:(推薦)

class Score:

    def __init__(self):
        self.score = 0

    def increment(self):
        self.score += 1

    def decrement(self):
        self.score -= 1

    def reset(self):
        self.score = 0

您可以按如下方式使用:

the_score = Score()
the_score.increment()
the_score.decrement()
the_score.reset()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM