【发布时间】:2018-01-04 04:44:21
【问题描述】:
我正在开发一个 Python/Kivy 应用程序,该应用程序应该向用户提出一系列数学问题。我的问题是我不知道如何重置为另一个问题或获取问题列表。这是我的 main.py 代码的 sn-p:
def answers(self):
"""Ask the specified number of questions, and return the number of
correct answers"""
self.correct_answers = []
self.incorrect_answers = []
self.ids.questions_easy.text = "2 + 2"
def check_answer1(self):
self.answers()
self.answer1 = self.ids.answers.text
score = 0
if self.answer1 == "4":
self.correct_answers.append(self.answer1)
self.ids.result_easy.text = 'Correct, Great Job!'
score += 1
self.ids.score_easy.text = str(score)
else:
self.incorrect_answers.append(self.answer1)
self.ids.result_easy.text = 'Incorrect, Better Luck Next Time!'
score -= 1
self.ids.score_easy.text = '-1'
如您所见,它提出了 2+2 是什么问题。然后将该答案存储在列表“self.correct_answers”中。如果它是错误的,它被存储在列表“self.incorrect_answers”中。我的问题是我不知道如何制作一系列 20 个左右的问题。我一直在尝试考虑诸如“for”循环之类的方法,或者类似的方法,但不确定我将如何去做。
让事情复杂化的是,我的 .kv 文件将问题放在 TextInput 框中,将答案放在另一个 TextInput 框中,同时将结果和分数放在两个单独的 TextInput 框中。总共有四个框一起工作,所以我不确定如何清除问题文本输入框,然后生成一个新问题,同时删除旧问题。例如,由于 2+2 是我的第一个问题,我不知道如何更改 TextInput 框以询问 3+3 是什么,或者 9x9。 下面是我的 .kv 代码的 sn-p:
<Easy_Level>:
name: 'Easy_1'
GridLayout:
rows: 6
spacing: 10
padding: 10
Label:
text: "Question"
TextInput:
id: questions_easy
text: ""
multiline: False
readonly: True
Label:
text: 'Answer'
TextInput:
id: answers
text: ""
multiline: False
Label:
text: 'Result'
TextInput:
id: result_easy
text:""
multiline: True
readonly: True
Label:
text: 'Score'
TextInput:
text: ""
id: score_easy
multiline: True
readonly: True
Button:
id: get_question
text: 'get question!'
on_release: root.answers()
size_hint: .1, .1
Button:
id: check_answer
text:'check answer!'
on_release: root.check_answer1()
size_hint: .1, .1
Button:
id: back_button
text: 'Back'
on_press: app.root.current = 'select_difficulty'
Button:
text: 'Continue'
如您所见,“question_easy”文本输入框可以提出问题,例如“什么是 2+2”,但我不知道如何删除该问题,然后再提出另一个问题。我想问总共 20-100 个问题,但我似乎只能产生 1 个问题。我不确定我是否会运行一个 for 循环,并创建一个问题列表,或者什么。我已经坚持了几个星期,而且对编程还很陌生。我希望我的问题足够详细,因为我知道没有什么比没有细节的问题更烦人的了。感谢您的帮助!
【问题讨论】:
-
我认为如果您从代码而不是 KV 文件构建 Kivy UI,这将更容易解决。然后,您可以创建一个包含问题列表的“问题小部件”类。您可以向此类添加一个方法来记录答案,然后在列表中显示下一个问题。