xtao_math_bot/challenge.py

81 lines
2.7 KiB
Python
Raw Normal View History

2020-03-05 06:54:56 +00:00
import random
import configparser
class Challenge:
"""
如果你是 Python 高手请继续如果你根本不会Python或者语法乱七八糟我建议你还是先去 https://docs.python.org 学习一个
一个简单的验证问题变量说明
请注意如果要使用中文写出各个选项建议把main.py里344-346行修改为如下形式
[InlineKeyboardButton(str(c),
callback_data=bytes(
str(c), encoding="utf-8"))])
这么做的目的是能让问题的选项每个一行否则一行四个答案可能会显示不下
__str__ 方法返回问题的具体内容,
qus 返回上面的 __str__ 方法
ans 返回 Challenge 的答案
choices 返回 Challenge 的选项们
_ans 属性是问题
_choices 是问题选项
比如说你可以这么改写 Challenge
def __init(self):
self._ans= ''
self._choices = []
self.new()
def __str__(self):
return "下面哪一个不是路由器的架构?"
def new(self):
self._choices['MIPS','ARM','8051','x86']
self._answer = '8051'
qus, ans,choices 这三个函数可以放着不动
"""
def __init__(self):
self._a = 0
self._pu = "A"
# 所以为啥要把a,b两个属性丢这里我不是太懂。。。
self._op = "A"
self._ans = 0
self._choices = []
self.new()
def __str__(self):
return "{op} {a}的正确选项是?".format(a=self._a, op=self._op)
def new(self):
conf = configparser.ConfigParser()
conf.read("tk.json")
operation = str(random.randint(0, int(conf.get("ans", "all"))))
a, ans = "号题目", "A"
try:
f = open(str("pic/") + operation + str(".png"))
f.close()
pu = str("pic/") + operation + str(".png")
except IOError:
pu = str("pic/") + operation + str(".jpg")
if operation in conf.get("ans", "A").split():
ans = "A"
elif operation in conf.get("ans", "B").split():
ans = "B"
elif operation in conf.get("ans", "C").split():
ans = "C"
elif operation in conf.get("ans", "D").split():
ans = "D"
choices = ['A','B','C','D']
self._a = a
self._op = operation
self._ans = ans
self._pu = pu
self._choices = choices
def qus(self):
return self.__str__()
def ans(self):
return self._ans
def pu(self):
return self._pu
def choices(self):
return self._choices