# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # 参考
# https://www.bilibili.com/video/av24416528

# + code_folding=[]
# 第一步，书写大框架
def main():
    # 定义题库列表，每道题目为一个字典
    question_list = [
        {
            'question':'生命、宇宙以及一切事务的终极答案是？',
            'choices':[
                '42',
                '43',
                '44',
                '45'
            ],
            'answer': 0
        },
        {
            'question':'机器人 Marvin 今天高兴吗？',
            'choices':[
                '没有理由不高兴',
                '他今天一定很犹忧郁',
                '取决于他接到的任务',
                '一会儿高兴，一会儿不高兴'
            ],
            'answer': 1            
        }
    ]

    print(question_list)
    # 遍历题库列表
        # 展示题目、提示用户输入、判断答案

    # 计算并展示正确率
    
if __name__ == '__main__':
    main()


# +
# 第二步，输出查看结果

def ask_question(question):
    pass

def main():
    # 定义题库列表，每道题目为一个字典
    question_list = [
        {
            'question':'生命、宇宙以及一切事务的终极答案是？',
            'choices':[
                '42',
                '43',
                '44',
                '45'
            ],
            'answer': 0
        },
        {
            'question':'机器人 Marvin 今天高兴吗？',
            'choices':[
                '没有理由不高兴',
                '他今天一定很犹忧郁',
                '取决于他接到的任务',
                '一会儿高兴，一会儿不高兴'
            ],
            'answer': 1            
        }
    ]

    print(question_list)
    
    # 遍历题库列表
    q_len = len(question_list)
    correct_count = 0
    for q in question_list:
        # 展示题目、提示用户输入、判断答案
        if ask_question(q):
            correct_count += 1
    # 计算并展示正确率
    print('答题完成，共{0}道题目，你答对了{1}题。正确率{2:.2f}%'.format(q_len,correct_count,correct_count/q_len*100))
    
if __name__ == '__main__':
    main()


# +
# 第三步，输出查看结果

def to_answer(letter):
    letter = letter.upper()
    if len(letter) == 1 and 'A' <= letter <= "Z":
        digit = ord(letter) - ord('A')
        return digit, True
    else:
        return None, False

def to_letter(n):
    return chr(ord('A') + n)

def ask_question(question, i):
    print('第{0}题：{1}'.format(i+1, question['question']))
    choices = question['choices']
    c_len = len(choices)
    for j in range(c_len):
          print('{0}:{1}'.format(to_letter(j), choices[j]))
    user_input = input('请输入答案：')
    user_answer, ok = to_answer(user_input)
    while not ok:
        user_input = input('输入错误，请输入选项前的单个字幕作为答案：')
        user_answer, ok = to_answer(user_input)
    return user_answer == question['answer']

def main():
    # 定义题库列表，每道题目为一个字典
    question_list = [
        {
            'question':'生命、宇宙以及一切事务的终极答案是？',
            'choices':[
                '42',
                '43',
                '44',
                '45'
            ],
            'answer': 0
        },
        {
            'question':'机器人 Marvin 今天高兴吗？',
            'choices':[
                '没有理由不高兴',
                '他今天一定很犹忧郁',
                '取决于他接到的任务',
                '一会儿高兴，一会儿不高兴'
            ],
            'answer': 1            
        }
    ]

#     print(question_list)
    
    # 遍历题库列表
    q_len = len(question_list)
    correct_count = 0
    for i,q in enumerate(question_list):
        # 展示题目、提示用户输入、判断答案
        if ask_question(q, i):
            correct_count += 1
    # 计算并展示正确率
    print('答题完成，共{0}道题目，你答对了{1}题。正确率{2:.2f}%'.format(q_len,correct_count,correct_count/q_len*100))
    
if __name__ == '__main__':
    main()


# +
# 第四步，修改选项数量不一致导致输入的问题

def to_answer(letter, c_len):
    letter = letter.upper()
    if len(letter) == 1 and 'A' <= letter <= "Z":
        digit = ord(letter) - ord('A')
        if 0 <= digit < c_len:
            return digit, True
        else:
            return None, False
    else:
        return None, False

def to_letter(n):
    return chr(ord('A') + n)

def ask_question(question, i):
    print('\n第{0}题：{1}'.format(i+1, question['question']))
    choices = question['choices']
    c_len = len(choices)
    for j in range(c_len):
          print('{0}:{1}'.format(to_letter(j), choices[j]))
    user_input = input('请输入答案：')
    user_answer, ok = to_answer(user_input, c_len)
    while not ok:
        user_input = input('\n输入错误，请输入选项前的单个字幕作为答案：')
        user_answer, ok = to_answer(user_input, c_len)
    return user_answer == question['answer']

def main():
    # 定义题库列表，每道题目为一个字典
    question_list = [
        {
            'question':'生命、宇宙以及一切事务的终极答案是？',
            'choices':[
                '42',
                '43',
                '44',
                '45'
            ],
            'answer': 0
        },
        {
            'question':'机器人 Marvin 今天高兴吗？',
            'choices':[
                '没有理由不高兴',
                '他今天一定很犹忧郁',
                '取决于他接到的任务',
                '一会儿高兴，一会儿不高兴'
            ],
            'answer': 1            
        }
    ]

#     print(question_list)
    
    # 遍历题库列表
    q_len = len(question_list)
    correct_count = 0
    for i,q in enumerate(question_list):
        # 展示题目、提示用户输入、判断答案
        if ask_question(q, i):
            correct_count += 1
    # 计算并展示正确率
    print('\n答题完成，共{0}道题目，你答对了{1}题。正确率{2:.2f}%'.format(q_len,correct_count,correct_count/q_len*100))
    
if __name__ == '__main__':
    main()


# +
# 第五步，读取外部JSON文件

def to_answer(letter, c_len):
    letter = letter.upper()
    if len(letter) == 1 and 'A' <= letter <= "Z":
        digit = ord(letter) - ord('A')
        if 0 <= digit < c_len:
            return digit, True
        else:
            return None, False
    else:
        return None, False

def to_letter(n):
    return chr(ord('A') + n)

def ask_question(question, i):
    print('\n第{0}题：{1}'.format(i+1, question['question']))
    choices = question['choices']
    c_len = len(choices)
    for j in range(c_len):
          print('{0}:{1}'.format(to_letter(j), choices[j]))
    user_input = input('请输入答案：')
    user_answer, ok = to_answer(user_input, c_len)
    while not ok:
        user_input = input('\n输入错误，请输入选项前的单个字幕作为答案：')
        user_answer, ok = to_answer(user_input, c_len)
    return user_answer == question['answer']

def main():
    # 定义题库列表，每道题目为一个字典
    question_list = [
        {
            'question':'生命、宇宙以及一切事务的终极答案是？',
            'choices':[
                '42',
                '43',
                '44',
                '45'
            ],
            'answer': 0
        },
        {
            'question':'机器人 Marvin 今天高兴吗？',
            'choices':[
                '没有理由不高兴',
                '他今天一定很犹忧郁',
                '取决于他接到的任务',
                '一会儿高兴，一会儿不高兴'
            ],
            'answer': 1            
        }
    ]

#     print(question_list)
    
    # 遍历题库列表
    q_len = len(question_list)
    correct_count = 0
    for i,q in enumerate(question_list):
        # 展示题目、提示用户输入、判断答案
        if ask_question(q, i):
            correct_count += 1
    # 计算并展示正确率
    print('\n答题完成，共{0}道题目，你答对了{1}题。正确率{2:.2f}%'.format(q_len,correct_count,correct_count/q_len*100))
    
if __name__ == '__main__':
    main()

StarcoderdataJupyterScriptsDedupFiltered