【无标题】EXCEL实现刷题

实现的思路:
导入题库word文件导入到excel–>绑定随机事件选定考题。
word题库导入Excel表的代码如下:
在这里插入图片描述
整理题库结构(添加题号,分离答案)
Public Sub numAdd()
Dim rng As Range, RNG1 As Range
With Sheet1
.[b1] = “题号”
.[c1] = “答案”
On Error Resume Next
For Each rng In .Range(“a2:a” & .Cells(Rows.Count, 1).End(xlUp).Row)
'rng = Replace(rng, chr(13), “”)
If qFind(rng) <> “” Then 'And qFind(rng) Like “[a-dA-D√?×Xx]” Then
counter = counter + 1
If qFind(rng) Like “[?√Vv]” Then
rng.Offset(0, 2) = “Y”
ElseIf qFind(rng) Like “[×Xx]” Then
rng.Offset(0, 2) = “N”
ElseIf Asc(qFind(rng)) = 63 Then
rng.Offset(0, 2) = “Y”
Else
rng.Offset(0, 2) = qFind(rng)
End If
rng = counter & ". " & Replace(rng.Value, qFind(rng), “”)
rng.Offset(0, 1) = counter
If rng.Row < .Columns(1).Find(“判断题:”).Row Then
rws = rng.End(xlDown).Row - rng.Row
’ Debug.Print rng & “|” & rws
If rws = 4 Then
For Each RNG1 In .Range(.Cells(rng.Row + 1, 1), .Cells(rng.End(xlDown).Row, 1))
ct = ct + 1
Select Case ct
Case Is = 1
RNG1 = "A. " & RNG1.Value
Case Is = 2
RNG1 = "B. " & RNG1.Value
Case Is = 3
RNG1 = "C. " & RNG1.Value
Case Is = 4
RNG1 = "D. " & RNG1.Value

                End Select
            Next
        ElseIf rws = 5 Then
            For Each RNG1 In .Range(.Cells(rng.Row + 1, 1), .Cells(rng.End(xlDown).Row, 1))
                ct = ct + 1
                Select Case ct
                    Case Is = 1
                        RNG1 = "A. " & RNG1.Value
                    Case Is = 2
                        RNG1 = "B. " & RNG1.Value
                    Case Is = 3
                        RNG1 = "C. " & RNG1.Value
                    Case Is = 4
                        RNG1 = "D. " & RNG1.Value
                    Case Is = 5
                        RNG1 = "E. " & RNG1.Value
                End Select
            Next
    
        ElseIf rws = 9 Then
            For Each RNG1 In .Range(.Cells(rng.Row + 1, 1), .Cells(rng.End(xlDown).Row, 1))
                ct = ct + 1
                Select Case ct
                    Case Is = 1
                        RNG1 = "A. " & RNG1.Value
                    Case Is = 2
                        RNG1 = "B. " & RNG1.Value
                    Case Is = 3
                        RNG1 = "C. " & RNG1.Value
                    Case Is = 4
                        RNG1 = "D. " & RNG1.Value
                    Case Is = 5
                    RNG1 = "E. " & RNG1.Value
                    Case Is = 6
                        RNG1 = "F. " & RNG1.Value
                    Case Is = 7
                        RNG1 = "G. " & RNG1.Value
                    Case Is = 8
                        RNG1 = "H. " & RNG1.Value
                    Case Is = 9
                        RNG1 = "I. " & RNG1.Value
                End Select
            Next
        End If
    End If
End If
ct = 0

Next
Sheet2.TextBox1.Text = counter
End With
End Sub
设计操作界面
在这里插入图片描述

设计操作界面
绑定点击操作事件
Private Sub CommandButton1_Click()
On Error Resume Next
Dim qnum As Integer, no As Integer

If Sheet2.OptionButton1.Value Then
selectQNum = Sheet1.Columns(1).Find(“判断题:”).Offset(0, 1).End(xlUp)
no = WorksheetFunction.RandBetween(1, selectQNum)
no = uniqueGen(no)
Call qSelect(no)
Sheet2.TextBox2.Text = no
[e1] = “已选题号”
Cells(Rows.Count, “e”).End(xlUp).Offset(1, 0) = no
Else
stNum = Sheet1.Columns(1).Find(“判断题:”).Offset(0, 1).End(xlDown)
endNum = Sheet1.Cells(Rows.Count, 2).End(xlUp)
no = WorksheetFunction.RandBetween(stNum, endNum)
no = uniqueGen(no)
Call qSelect(no)
Sheet2.TextBox2.Text = no
[e1] = “已选题号”
Cells(Rows.Count, “e”).End(xlUp).Offset(1, 0) = no
End If
End Sub

创建非重复题目选择函数
Public Function uniqueGen(no)
On Error Resume Next
selectQNum = Sheet1.Columns(1).Find(“判断题:”).Offset(0, 1).End(xlUp)
For i = 1 To selectQNum
If Sheet2.OptionButton1.Value = True Then
no = WorksheetFunction.RandBetween(1, selectQNum)
If Sheet2.Columns(“e”).Find(no).Row = “” Then
uniqueGen = no
Exit For
End If
Else
stNum = Sheet1.Columns(1).Find(“判断题:”).Offset(0, 1).End(xlDown)
endNum = Sheet1.Cells(Rows.Count, 2).End(xlUp)
no = WorksheetFunction.RandBetween(stNum, endNum)
If Sheet2.Columns(“e”).Find(no).Row = “” Then
uniqueGen = no
Exit For
End If
End If
Next
End Function
给“查看答案”添加执行程序,代码如下:
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python用Excel刷题程序的实现可以借助tkinter库。首先,我们需要导入`tkinter`和`openpyxl`库,`tkinter`用于创建图形用户界面,`openpyxl`用于读取和操作Excel文件。 1. 创建一个主窗口: ```python import tkinter as tk root = tk.Tk() root.title("Excel刷题程序") root.geometry("500x500") ``` 2. 选择Excel文件: ```python from tkinter import filedialog def select_file(): file_path = filedialog.askopenfilename(filetypes=(('Excel files', '*.xlsx'), ('All files', '*.*'))) # 对选择的Excel文件进行处理 # ... select_file_button = tk.Button(root, text="选择Excel文件", command=select_file) select_file_button.pack() ``` 通过`filedialog.askopenfilename`函数选择Excel文件,并将文件路径存储在`file_path`变量中,接下来可以对该文件进行处理。 3. 读取Excel文件并进行题目展示: ```python import openpyxl def read_excel(file_path): wb = openpyxl.load_workbook(file_path) ws = wb.active for row in ws.iter_rows(min_row=2): question = row[0].value # 假设题目保存在第一列 # 题目处理 print(question) # 在select_file函数中调用read_excel函数 # ... read_excel(file_path) ``` 使用`openpyxl.load_workbook`函数加载Excel文件,然后通过`wb.active`选择活动的工作表,使用`iter_rows`方法遍历每一行,获取题目的内容并进行处理。 4. 添加答题按钮和答案判定: ```python def check_answer(index, answer): # 获取对应行的答案并进行比较 # ... def show_question(index, question): question_label = tk.Label(root, text=str(index) + ". " + question) question_label.pack() answer_entry = tk.Entry(root) answer_entry.pack() check_button = tk.Button(root, text="答题", command=lambda: check_answer(index, answer_entry.get())) check_button.pack() ``` `check_answer`函数用于获取选择的答案并与正确答案比较。`show_question`函数用于在界面上展示题目和答题选项。 最后,我们需要处理题目的展示顺序和判断答案正确与否的逻辑,这里只展示了基本的框架。根据具体需求,我们还可以添加提交按钮、计分功能等。 以上是一个基于tkinter和openpyxl库实现的Python刷题程序,通过这样的程序,我们可以从Excel文件中读取题目,并在图形界面中展示,并且可以选择答案并进行判定。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值