【发布时间】:2018-07-13 16:19:53
【问题描述】:
我正在尝试制作一个询问 25 个问题的 Python 测验。我用字典来保存问题,用一个键。我已经使用 random.randint() 函数从字典中选择一个随机问题,我的目标是,一旦问题得到回答,就从字典中删除该问题,使其无法再次回答。我没有编写决定答案是否正确的逻辑。这是重要的代码:
def delete_question():
try:
del questions[chosen_question]
except KeyError:
pass
question_number -= 1
random_question.update()
root_win2()
def root_win2():
global questions, chosen_question, random_question, question_number
question_number = 0
questions = {
1: "Describe what the instruction 'BRP' does.",
2: "Describe what is meant by the term 'Open Source Software'.",
3: "What is meant by the term 'Lossy Compression'?",
4: "What is the number '55' as an 8-bit unsigned integer?",
5: "What might a printer use RAM for?",
6: "Describe the term 'firewall'.",
7: "Describe the Rapid Application Development process."
}
question_number = 7
If question_number > 0:
chosen_question = random.randint(1, question_number)
random_question = Label(root, bg="white", text=(questions [chosen_question]), font = ('Courier', 13))
random_question.place(x=10, y=50)
delete_button = Button(text="Next", command=delete_question, height=3, width=12)
delete_button.place(x=370, y = 420)
我应该如何编辑我的代码,以便在提出问题时不再询问?
如果有人需要,这里是完整的代码:
from tkinter import *
import time
import random
def destroy():
random_question = Label()
close_button.destroy()
about_button.destroy()
start_button.destroy()
version.destroy()
conf.destroy()
root.title("Computer Science Quiz")
random_question.destroy()
root_win2()
def delete_question():
question_number = 0
try:
del questions[chosen_question]
except KeyError:
pass
question_number -= 1
random_question.update()
root_win2()
def root_win2():
global questions, chosen_question, random_question, question_number
question_number = 0
questions = {
1: "Describe what the instruction 'BRP' does.",
2: "Describe what is meant by the term 'Open Source Software'.",
3: "What is meant by the term 'Lossy Compression'?",
4: "What is the number '55' as an 8-bit unsigned integer?",
5: "What might a printer use RAM for?",
6: "Describe the term 'firewall'.",
7: "Describe the Rapid Application Development process."
}
question_number = 7
if question_number > 0:
chosen_question = random.randint(1, question_number)
random_question = Label(root, bg="white", text=(questions[chosen_question]), font = ('Courier', 13))
random_question.place(x=10, y=50)
delete_button = Button(text="Next", command=delete_question, height=3, width=12)
delete_button.place(x=370, y = 420)
def root_win1():
global root, close_button, about_button, start_button, version, root
root = Tk()
root.title("Start Menu")
root.geometry("800x800")
root.configure(background='white')
root.resizable(width=False, height=False)
close_button = Button(text="Quit", fg="red", command=quit, height=3, width=12)
close_button.place(x=370, y = 420)
about_button = Button(text="About", fg="blue", command=new_win, height=3, width=12)
about_button.place(x=370, y=360)
start_button = Button(text="Begin", fg="green", command=confirmation, height=3, width=12)
start_button.place(x=370, y=300)
version = Label(root, bg="white", text="v0.48")
version.place(x=767, y=0)
def callback(event):
webbrowser.open_new(r"http://www.ocr.org.uk/Images/170845-specification-accredited-as-level-gce-computer-science-h046.pdf")
def new_win():
about = Toplevel()
about.title("About")
about.geometry("370x300")
about.configure(background='white')
about.resizable(width=False, height=False)
about_text = Label(about, bg="white", anchor=W, justify=LEFT, text = "OCR AS Level Computer Science Quiz \n\n"
"This app quizzes you on 25 random questions related to computing, \n"
"in accordance with the OCR AS Computer Science specification \n\n"
"You are given 10 minutes to answer 25 questions \n\n"
"Version 0.48 \n\n"
"Firas Hafiz - 2018 \n\n"
"You can visit the subject specification from the link below: \n")
about_text.pack()
back_button1 = Button(about, text="Understood", command=about.destroy, height=2, width=10)
back_button1.place(x=145, y=240)
link = Label(about, bg="white", justify=LEFT, anchor=W, text="OCR AS Level Computer Science Specification", fg="blue", cursor="hand2")
link.pack()
link.bind("<Button-1>", callback)
about.focus_set()
about.grab_set()
about.mainloop()
def confirmation():
global conf
conf = Toplevel()
conf.title("Begin?")
conf.geometry("250x100")
conf.configure(background='white')
conf.resizable(width=False, height=False)
conf_label = Label(conf, bg="white", justify=LEFT, anchor=W, text="Are you sure you want to continue?")
conf_label.pack()
continue_button = Button(conf, text="Continue", command=destroy, height=2, width=10)
continue_button.place(x=30, y=30)
back_button2 = Button(conf, text="Go back", command=conf.destroy, height=2, width=10)
back_button2.place(x=136, y=30)
conf.focus_set()
conf.grab_set()
conf.mainloop()
root_win1()
root.mainloop()
【问题讨论】:
-
你能把它减少到minimal reproducible example,而不是转储你所有的代码吗?另外,请在一个问题中只问一个问题;你的 tkinter 问题完全不相关,不能用相同的答案来回答,所以应该单独发布。 (您可以随时复制“共享”链接并将它们粘贴到另一个问题中,以说明它们是不同的问题但代码相同,因此另一个问题可能与阅读的任何人相关。)
-
无论如何,这里有什么问题?看起来您的
delete_question函数应该删除问题。我不确定它为什么要做其他一些事情,比如创建一个名为question_number的本地,它设置为 0,然后设置为 -1 并且从不查看,但这不应该破坏任何东西。 -
最后:直接
random.shuffle提问,然后按(打乱)顺序提问,不用担心删除任何内容,会不会简单很多? -
它没有删除问题,我无法确定问题所在,但是当我运行它时,它一直在问问题,就好像它们还在那里一样。