前一篇對 deepseek-r1:14b 測試發現, 它很擅長推理, 聊天對話都很不錯, 但叫它寫個簡單的四則運算計算器程式卻一直出錯, Gemini 與 ChatGPT 建議可以試試 deepseek-coder-v2-lite-instruct 模型, 它也是為 Coding 而訓練的模型.
不過, 適合 RTX3060 12GB 顯卡下載的 deepseek-coder-v2-lite-instruct 模型有兩種, 都是 Q4_K_M 量化的 10GB 模型 :
dagbs 版比較像原汁原味高品質 GGUF 封裝; 而 Mannix 版則是社群為 AI IDE/agent workflow 專門優化的魔改版, 主要是針對長對話遺忘系統提示與上下文問題加以修正. 以下先下載 mannix 版來測試 :
PS C:\Users\USER> ollama run mannix/deepseek-coder-v2-lite-instruct:q4_k_m
pulling manifest
pulling 35cee930b4ba: 100% ▕███████████████████████████████████████▏ 10 GB
pulling 60f3b49c3739: 100% ▕███████████████████████████████████████▏ 210 B
pulling 4bb71764481f: 100% ▕███████████████████████████████████████▏ 13 KB
pulling 1c8f573e830c: 100% ▕████████████████████████████████████████▏ 1.1 KB
pulling a8e590afd870: 100% ▕████████████████████████████████████████▏ 148 B
pulling baf19e532358: 100% ▕████████████████████████████████████████▏ 570 B
verifying sha256 digest
writing manifest
success
先打招呼 :
>>> 你是誰?
我是DeepSeek Coder,一个由深度求索公司开发的智能助手,旨在通过自然语言处理和机器学习技术来提供信息查询、对话交流和解答问题等服务
。
規範用繁中回應 :
>>> 請一律使用繁體中文與我交談
了解您的需求。我将使用繁体中文与您交谈。请随时告诉我您的问题或需要的信息,我会尽力为您提供帮助。
問量子力學 :
>>> 請簡介量子糾纏
量子糾纏(Quantum Entanglement)是指兩個或更多量子系統之間的狀態高度相關聯,即使它們被分隔在遠距離的情況下,也會瞬時相互影響。這
種現象是量子力學的基本特性之一,挑戰了經典物理的直覺概念。
當兩個量子糾纏的粒子互相作用後,它們的狀態會變成一個整體,稱為聯合量子態或糾纏態。這意味著對其中一個粒子的測量可以確定地預言另一
個粒子的狀態,無論它們相距多遠。這種非局域的相互作用在經典物理中是不可能的,因此量子糾纏被視為量子通信和量子計算中的重要資源。
以上測試可知此模型比 deepseek-r1:14b 回應叫簡潔與迅速.
改問它一個數學問題 :
>>> 3.10 與 3.8 這兩個數值哪一個比較大?
...
在比較 3.10 和 3.8 這兩個數值時,顯然 3.10 比 3.8 更大。
顯然數學程度不行喔. 問個簡單點的 :
>>> 一支筆 3 元,一本筆記本比筆貴 7 元。買一支筆和一本筆記本共多少元?
...
首先,我們知道一支筆的價格是 3 元。
然後,我們知道一本筆記本的價格比筆貴 7 元,所以一本筆記本的價格是 3 + 7 = 10 元。
最後,我們把一支筆和一本筆記本的價格加起來,就是 3 + 10 = 13 元。
所以,買一支筆和一本筆記本共需要 13 元。
這題答對了. 來一題平均數的 :
>>> 小明第一次考 80 分,第二次考幾分,平均才會變成 90 分?
要找出小明第二次考試的分數,使得他的平均分變成 90 分,我們可以設小明第二次考試的成績為 \( x \)。然後根據題目信息建立等式:
\[
\frac{80 + x}{2} = 90
\]
接下來,我們解這個方程。首先,將兩邊同時乘以 2:
\[
80 + x = 180
\]
然後,解這個方程以求 \( x \):
\[
x = 180 - 80
\]
\[
x = 100
\]
所以,小明第二次考試需要考 100 分,才能使他的平均分變成 90 分。
答對囉.
考它的程式能力 :
>>> 請用 Python 寫一個基本的 GUI 計算器程式
當然可以!以下是一個簡單的 Python GUI 計算器的示例,使用 `tkinter` 模塊來創建圖形用戶界面。
```python
import tkinter as tk
from tkinter import messagebox
def add():
try:
num1 = float(entry_num1.get())
num2 = float(entry_num2.get())
result.set(num1 + num2)
except ValueError:
messagebox.showerror("錯誤", "請輸入有效的數字")
def subtract():
try:
num1 = float(entry_num1.get())
num2 = float(entry_num2.get())
result.set(num1 - num2)
except ValueError:
messagebox.showerror("錯誤", "請輸入有效的數字")
def multiply():
try:
num1 = float(entry_num1.get())
num2 = float(entry_num2.get())
result.set(num1 * num2)
except ValueError:
messagebox.showerror("錯誤", "請輸入有效的數字")
def divide():
try:
num1 = float(entry_num1.get())
num2 = float(entry_num2.get())
if num2 == 0:
messagebox.showerror("錯誤", "除數不能為零")
else:
result.set(num1 / num2)
except ValueError:
messagebox.showerror("錯誤", "請輸入有效的數字")
# 創建主窗口
root = tk.Tk()
root.title("簡易計算器")
# 創建輸入框和結果顯示
frame = tk.Frame(root)
frame.pack(padx=10, pady=10)
entry_num1 = tk.Entry(frame, width=20)
entry_num1.grid(row=0, column=1, padx=5, pady=5)
entry_num2 = tk.Entry(frame, width=20)
entry_num2.grid(row=1, column=1, padx=5, pady=5)
result = tk.StringVar()
label_result = tk.Label(frame, textvariable=result, width=20)
label_result.grid(row=2, column=1, padx=5, pady=5)
# 創建按鈕
btn_add = tk.Button(root, text="+", command=add)
btn_add.pack(padx=10, pady=10)
btn_subtract = tk.Button(root, text="-", command=subtract)
btn_subtract.pack(padx=10, pady=10)
btn_multiply = tk.Button(root, text="*", command=multiply)
btn_multiply.pack(padx=10, pady=10)
btn_divide = tk.Button(root, text="/", command=divide)
btn_divide.pack(padx=10, pady=10)
# 運行主循環
root.mainloop()
```
這段代碼創建了一個簡單的計算器,支持加、減、乘、除四種基本操作。用戶可以輸入兩個數字,然後點擊相應的按鈕來進行計算。如果有任何無
效的輸入(如非數字的字符),程序會顯示一個錯誤信息框。
結果如下, 與預期的版面不同, 運算元是輸入在欄位中, 而不是按 0~9 按鈕 :
要求它照一般計算器版面修改 :
>>> 不是這樣, 請模仿真實計算器排版來改寫, 有 0~9 與加減乘除等於這些按鈕
好的,以下是一個更接近真實計算器外觀的 Python GUI 計算器的示例:
```python
import tkinter as tk
from tkinter import messagebox
def add(event=None):
calculate("+")
def subtract(event=None):
calculate("-")
def multiply(event=None):
calculate("*")
def divide(event=None):
calculate("/")
def calculate(operator):
try:
num1 = float(entry_num1.get())
num2 = float(entry_num2.get())
if operator == "+":
result.set(num1 + num2)
elif operator == "-":
result.set(num1 - num2)
elif operator == "*":
result.set(num1 * num2)
elif operator == "/":
if num2 == 0:
messagebox.showerror("錯誤", "除數不能為零")
else:
result.set(num1 / num2)
except ValueError:
messagebox.showerror("錯誤", "請輸入有效的數字")
# 創建主窗口
root = tk.Tk()
root.title("計算器")
# 創建輸入框和結果顯示
entry_num1 = tk.Entry(root, width=20)
entry_num1.grid(row=0, column=0, columnspan=4, padx=5, pady=5)
result = tk.StringVar()
label_result = tk.Label(root, textvariable=result, width=20, borderwidth=1, relief="solid")
label_result.grid(row=1, column=0, columnspan=4, padx=5, pady=5)
# 創建數字按鈕
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+',
]
row = 2
col = 0
for button in buttons:
if button == "=":
btn_equal = tk.Button(root, text=button, width=10, command=lambda: calculate("+"))
btn_equal.grid(row=row, column=col, columnspan=2, padx=5, pady=5)
elif button == "0":
btn_zero = tk.Button(root, text=button, width=10)
btn_zero.grid(row=row, column=col, columnspan=2, padx=5, pady=5)
col += 1
elif button == ".":
btn_dot = tk.Button(root, text=button, width=10)
btn_dot.grid(row=row, column=col, padx=5, pady=5)
else:
btn = tk.Button(root, text=button, width=10)
btn.grid(row=row, column=col, padx=5, pady=5)
col += 1
if col > 3:
col = 0
row += 1
# 綁定按鈕事件
btn_add = tk.Button(root, text="+", width=10, command=lambda: calculate("+"))
btn_add.grid(row=5, column=2, padx=5, pady=5)
btn_subtract = tk.Button(root, text="-", width=10, command=lambda: calculate("-"))
btn_subtract.grid(row=5, column=3, padx=5, pady=5)
btn_multiply = tk.Button(root, text="*", width=10, command=lambda: calculate("*"))
btn_multiply.grid(row=6, column=2, padx=5, pady=5)
btn_divide = tk.Button(root, text="/", width=10, command=lambda: calculate("/"))
btn_divide.grid(row=6, column=3, padx=5, pady=5)
# 運行主循環
root.mainloop()
```
這段代碼創建了一個具有0-9數字鍵、加減乘除鍵和等號的計算器。用戶可以通過點擊按鈕或使用鍵盤輸入來進行計算。如果需要使用鍵盤上的數字
鍵,可以直接在輸入框中輸入,或者直接點擊對應的數字按鈕。
乖乖, 修正版很怪, 可以執行卻無法使用 :
再次要求修改 :
>>> 不是啦, 你這個修正版怎麼有兩個 + 與 * 按鈕, +-*/ 按鈕都只有一個, 使用者輸入數值與運算符時要顯示在輸入框, 按下 = 顯示結果, 按 AC 清除輸入框
好的,以下是根據您的需求修改後的 Python GUI 計算器代碼:
```python
import tkinter as tk
from tkinter import messagebox
def add(event=None):
calculate("+")
def subtract(event=None):
calculate("-")
def multiply(event=None):
calculate("*")
def divide(event=None):
calculate("/")
def calculate(operator):
try:
num1 = float(entry_num.get())
if operator == "=":
result.set("") # Clear the entry box after calculation
else:
current_operation.set(operator)
entry_num.delete(0, tk.END)
entry_num.insert(tk.END, "")
except ValueError:
messagebox.showerror("錯誤", "請輸入有效的數字")
def equal():
try:
num2 = float(entry_num.get())
if current_operation.get() == "+":
result.set(str(num1 + num2))
elif current_operation.get() == "-":
result.set(str(num1 - num2))
elif current_operation.get() == "*":
result.set(str(num1 * num2))
elif current_operation.get() == "/":
if num2 == 0:
messagebox.showerror("錯誤", "除數不能為零")
else:
result.set(str(num1 / num2))
except ValueError:
messagebox.showerror("錯誤", "請輸入有效的數字")
def clear():
entry_num.delete(0, tk.END)
result.set("")
current_operation.set("")
# 創建主窗口
root = tk.Tk()
root.title("計算器")
# 創建輸入框和結果顯示
entry_num = tk.Entry(root, width=20)
entry_num.grid(row=0, column=0, columnspan=4, padx=5, pady=5)
result = tk.StringVar()
label_result = tk.Label(root, textvariable=result, width=20, borderwidth=1, relief="solid")
label_result.grid(row=1, column=0, columnspan=4, padx=5, pady=5)
# 創建數字按鈕
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+',
]
row = 2
col = 0
for button in buttons:
if button == "=":
btn_equal = tk.Button(root, text=button, width=10, command=equal)
btn_equal.grid(row=row, column=col, padx=5, pady=5)
elif button == "AC":
btn_clear = tk.Button(root, text=button, width=10, command=clear)
btn_clear.grid(row=row, column=col + 1, padx=5, pady=5)
else:
btn = tk.Button(root, text=button, width=10, command=lambda b=button: entry_num.insert(tk.END, b))
btn.grid(row=row, column=col, padx=5, pady=5)
col += 1
if col > 3:
col = 0
row += 1
# 創建運算符按鈕
current_operation = tk.StringVar()
btn_add = tk.Button(root, text="+", width=10, command=lambda: calculate("+"))
btn_subtract = tk.Button(root, text="-", width=10, command=lambda: calculate("-"))
btn_multiply = tk.Button(root, text="*", width=10, command=lambda: calculate("*"))
btn_divide = tk.Button(root, text="/", width=10, command=lambda: calculate("/"))
btn_add.grid(row=2, column=3, padx=5, pady=5)
btn_subtract.grid(row=3, column=3, padx=5, pady=5)
btn_multiply.grid(row=4, column=3, padx=5, pady=5)
btn_divide.grid(row=5, column=3, padx=5, pady=5)
# 運行主循環
root.mainloop()
```
這個版本的計算器有以下特點:
1. 數字鍵和運算符鍵分別處理。
2. 使用者輸入的數值和運算符會顯示在輸入框中。
3. 按下等於鈕 (`=`) 後,計算結果會顯示在輸入框中,並清除輸入框內容。
4. 按下清除鍵 (`AC`) 後,會清空輸入框和運算符。
結果越改越糟, 按 12+13= 竟然拋出例外 :
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\tony1\AppData\Local\Programs\Thonny\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "<string>", line 32, in equal
NameError: name 'num1' is not defined
也沒看到 AC 鍵啊, 我看還是算了.
下面我改下載 dagbs/deepseek-coder-v2-lite-instruct:q4_k_m 模型來試試 :
PS C:\Users\USER> ollama run dagbs/deepseek-coder-v2-lite-instruct:q4_k_m
pulling manifest
pulling 603bd3f8a028: 100% ▕██████████████████████████████████████████▏ 10 GB
pulling f5a4804891a6: 100% ▕██████████████████████████████████████████▏ 152 B
pulling 921a1f9def03: 100% ▕██████████████████████████████████████████▏ 59 B
pulling 6741a9985f0d: 100% ▕██████████████████████████████████████████▏ 422 B
verifying sha256 digest
writing manifest
success
直接叫它寫程式 :
>>> 請用 Python 寫一個基本的 GUI 四則運算計算器程式
當然可以!以下是一個簡單的 Python GUI 四則運算計算器的示例程式。我們將使用 `tkinter` 來創建 GUI。
```python
import tkinter as tk
from tkinter import messagebox
def calculate():
try:
num1 = float(entry_num1.get())
num2 = float(entry_num2.get())
operator = operator_var.get()
if operator == '+':
result.set(num1 + num2)
elif operator == '-':
result.set(num1 - num2)
elif operator == '*':
result.set(num1 * num2)
elif operator == '/':
if num2 != 0:
result.set(num1 / num2)
else:
messagebox.showerror("錯誤", "除數不能為零")
except ValueError:
messagebox.showerror("錯誤", "請輸入有效的數字")
# 創建主窗口
root = tk.Tk()
root.title("四則運算計算器")
# 創建變量來存儲用戶輸入和結果
result = tk.DoubleVar()
# 創建輸入框
entry_num1 = tk.Entry(root, width=10)
entry_num1.grid(row=0, column=0, padx=5, pady=5)
entry_num2 = tk.Entry(root, width=10)
entry_num2.grid(row=0, column=2, padx=5, pady=5)
# 創建操作符選擇框
operator_var = tk.StringVar()
operator_var.set('+')
tk.Radiobutton(root, text='+', variable=operator_var, value='+').grid(row=0, column=1)
tk.Radiobutton(root, text='-', variable=operator_var, value='-').grid(row=1, column=1)
tk.Radiobutton(root, text='*', variable=operator_var, value='*').grid(row=2, column=1)
tk.Radiobutton(root, text='/', variable=operator_var, value='/').grid(row=3, column=1)
# 創建計算按鈕
calculate_button = tk.Button(root, text="計算", command=calculate)
calculate_button.grid(row=4, column=0, columnspan=3, pady=10)
# 創建結果顯示框
result_label = tk.Label(root, textvariable=result)
result_label.grid(row=5, column=0, columnspan=3)
# 運行主循環
root.mainloop()
```
這個程式包含以下幾部分:
1. **創建主窗口**:使用 `tk.Tk()` 創建主窗口,並設置標題。
2. **創建輸入框**:用於輸入兩個數字。
3. **創建操作符選擇框**:提供加、減、乘、除四種操作符供選擇。
4. **創建計算按鈕**:當用戶點擊按鈕時,執行計算操作。
5. **顯示結果**:將計算結果顯示在窗口中。
你可以運行這個程式並嘗試輸入數字和選擇操作符來進行四則運算。
跟上面 mannix 版一樣, 生成的程式可用, 但不是預期的計算器版面 :
叫它修改 :
>>> 不是這樣, 請模仿真實計算器排版來改寫, 有 0~9 與 +-*/=, AC 等按鈕, 使用者輸入數值與運算符時要顯示在輸入框, 按下 = 顯示結果, 按 AC
... 清除輸入框
好的,以下是一個更接近真實計算器外觀的 Python GUI 四則運算計算器的示例程式:
```python
import tkinter as tk
from tkinter import messagebox
def calculate(entry, result):
try:
num1 = float(entry.get())
operator = operator_var.get()
if operator == '+':
return num1 + float(next_num.get())
elif operator == '-':
return num1 - float(next_num.get())
elif operator == '*':
return num1 * float(next_num.get())
elif operator == '/':
if float(next_num.get()) != 0:
return num1 / float(next_num.get())
else:
messagebox.showerror("錯誤", "除數不能為零")
return None
except ValueError:
messagebox.showerror("錯誤", "請輸入有效的數字")
return None
except TypeError:
return num1
def button_click(value):
current = entry.get()
if value == 'AC':
entry.delete(0, tk.END)
elif value == '=':
result = calculate(entry, next_num)
if result is not None:
entry.delete(0, tk.END)
entry.insert(tk.END, str(result))
else:
entry.delete(0, tk.END)
entry.insert(tk.END, current + value)
# 創建主窗口
root = tk.Tk()
root.title("四則運算計算器")
# 創建輸入框
entry = tk.Entry(root, width=20, font=('Arial', 14))
entry.grid(row=0, column=0, columnspan=4, padx=5, pady=5)
# 創建按鈕
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+',
'AC'
]
row_index, col_index = 1, 0
for button in buttons:
tk.Button(root, text=button, width=5, height=2, font=('Arial', 14), command=lambda b=button:
button_click(b)).grid(row=row_index, column=col_index)
col_index += 1
if col_index > 3:
col_index = 0
row_index += 1
# 創建變量來存儲用戶輸入和結果
operator_var = tk.StringVar()
next_num = tk.StringVar()
# 運行主循環
root.mainloop()
```
這個程式包含以下幾部分:
1. **創建主窗口**:使用 `tk.Tk()` 創建主窗口,並設置標題。
2. **創建輸入框**:用於顯示和輸入數字。
3. **創建按鈕**:包含數字按鈕(0-9)、運算符按鈕(+-*/)、等號(=)和清除(AC)按鈕。
4. **處理按鈕點擊事件**:當用戶點擊數字或運算符時,更新輸入框;當用戶點擊等號時,計算結果並顯示在輸入框中;當用戶點擊清除按鈕時,
清空輸入框。
你可以運行這個程式並嘗試使用它來進行四則運算。
這次版面大致符合要求了, 但按 = 鍵卻出現 "請輸入有效的數字" :
繼續修正 :
>>> 外觀改對了,但按=沒有進行計算啊, 而是顯示請輸入有效數字
抱歉給您帶來不便。我發現在之前的程式碼中,當用戶按下等號(=)時,會出現錯誤訊息「請輸入有效的數字」。這是因為我在 `calculate` 函
數中使用了兩個變量 `num1` 和 `next_num`,而沒有正確處理它們之間的關係。我將修復這個問題並提供一個新的程式碼版本。
以下是修正後的程式碼:
```python
import tkinter as tk
from tkinter import messagebox
def calculate(entry):
try:
expression = entry.get()
result = eval(expression)
return result
except Exception as e:
messagebox.showerror("錯誤", "請輸入有效的數字")
return None
def button_click(value):
current = entry.get()
if value == 'AC':
entry.delete(0, tk.END)
elif value == '=':
result = calculate(entry)
if result is not None:
entry.delete(0, tk.END)
entry.insert(tk.END, str(result))
else:
entry.insert(tk.END, value)
# 創建主窗口
root = tk.Tk()
root.title("四則運算計算器")
# 創建輸入框
entry = tk.Entry(root, width=20, font=('Arial', 14))
entry.grid(row=0, column=0, columnspan=4, padx=5, pady=5)
# 創建按鈕
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+',
'AC'
]
row_index, col_index = 1, 0
for button in buttons:
tk.Button(root, text=button, width=5, height=2, font=('Arial', 14), command=lambda b=button:
button_click(b)).grid(row=row_index, column=col_index)
col_index += 1
if col_index > 3:
col_index = 0
row_index += 1
# 運行主循環
root.mainloop()
```
這個版本的程式碼中,當用戶按下等號(=)時,會將輸入框中的整個算式傳遞給 `eval` 函數來計算結果。`eval` 函數可以解析並執行字串形式
的 Python 表達式。這樣一來,就可以正確地計算四則運算的結果而不會出現錯誤。
希望這次的修改能解決您的問題!如果有其他需要改進的地方,請隨時告知。
這次終於改好了 :
雖然沒有一步到位給出正確可用的程式碼 (或許是提示詞寫得不夠精確), deepseek-coder-v2-lite-instruct 模型回應速度倒是不錯, 而且我覺得 Gemini/ChatGPT 推薦的 mannix 版反而不如 dagbs 版.
下面是在 dagbs 版模型用較精確的需求描述的測試, 確實一次就搞定 :
>>> 請用 Python 寫一個基本的 GUI 四則運算計算器程式, 請模仿真實計算器排版, 有 0~9 與 +-*/=, AC 等按鈕, 使用者按鈕輸入數值與運算符時要
... 顯示在輸入框, 按下 = 顯示計算結果, 按 AC 清除輸入框
好的,以下是一個基本的 Python GUI 四則運算計算器的程式碼。這個程式模仿了真實計算器的排版,包含數字按鈕(0-9)、運算符按鈕(+-*/)
、等號(=)和清除(AC)按鈕。使用者按下按鈕時會在輸入框中顯示輸入的數值與運算符,按下等號會顯示計算結果,按 AC 則清除輸入框。
```python
import tkinter as tk
from tkinter import messagebox
def calculate(entry):
try:
expression = entry.get()
result = eval(expression)
return str(result)
except Exception as e:
return "Error"
def button_click(value, entry):
if value == 'AC':
entry.delete(0, tk.END)
elif value == '=':
result = calculate(entry)
if result != "Error":
entry.delete(0, tk.END)
entry.insert(tk.END, result)
else:
current = entry.get()
entry.delete(0, tk.END)
entry.insert(tk.END, current + value)
# 創建主窗口
root = tk.Tk()
root.title("四則運算計算器")
# 創建輸入框
entry = tk.Entry(root, width=20, font=('Arial', 14))
entry.grid(row=0, column=0, columnspan=4, padx=5, pady=5)
# 創建按鈕
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+',
'AC'
]
row_index, col_index = 1, 0
for button in buttons:
tk.Button(root, text=button, width=5, height=2, font=('Arial', 14), command=lambda b=button: button_click(b,
entry)).grid(row=row_index, column=col_index)
col_index += 1
if col_index > 3:
col_index = 0
row_index += 1
# 運行主循環
root.mainloop()
```
這個程式碼使用了 `tkinter` 模組來創建 GUI,並使用 `eval` 函數來計算表達式的值。請注意,直接使用 `eval` 可能會帶來安全風險,特別是
在網路應用程式中。在實際應用中,您可能需要考慮更安全的替代方案來評估表達式。
結果版面與功能均如預期般正確 :
跳出 dagbs 版模型, 回頭重新載入 mannix 版模型用較精確的提示詞重測 :
>>> /exit
PS C:\Users\USER> ollama run mannix/deepseek-coder-v2-lite-instruct:q4_k_m
>>> 請用 Python 寫一個基本的 GUI 四則運算計算器程式, 請模仿真實計算器排版, 有 0~9 與 +-*/=, AC 等按鈕, 使用者按鈕輸入數值與運算符時要
... 顯示在輸入框, 按下 = 顯示計算結果, 按 AC 清除輸入框
當然!以下是一個簡單的 Python GUI 四則運算計算器的程式碼,模仿真實計算器排版,並包含 0~9、+-*/=、AC 等按鈕。
```python
import tkinter as tk
from tkinter import messagebox
def button_click(number):
current = entry.get()
entry.delete(0, tk.END)
entry.insert(tk.END, current + str(number))
def clear():
entry.delete(0, tk.END)
def calculate():
try:
result = eval(entry.get())
entry.delete(0, tk.END)
entry.insert(tk.END, result)
except Exception as e:
messagebox.showerror("Error", "Invalid input")
# 建立主視窗
root = tk.Tk()
root.title("四則運算計算器")
# 建立輸入框
entry = tk.Entry(root, width=35, borderwidth=5)
entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
# 建立按鈕
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+',
'AC'
]
row_index = 1
col_index = 0
for button in buttons:
if button == '=':
btn = tk.Button(root, text=button, padx=40, pady=20, command=calculate)
elif button == 'AC':
btn = tk.Button(root, text=button, padx=40, pady=20, command=clear)
else:
btn = tk.Button(root, text=button, padx=40, pady=20, command=lambda b=button: button_click(b))
btn.grid(row=row_index, column=col_index)
col_index += 1
if col_index > 4:
col_index = 0
row_index += 1
# 啟動主迴圈
root.mainloop()
```
這個程式碼使用 `tkinter` 模組來建立一個基本的 GUI 計算器。以下是一些重要的部分:
1. **按鈕功能**:
- `button_click(number)`: 用於在輸入框中顯示數字按鈕的點擊。
- `clear()`: 清除輸入框中的所有內容。
- `calculate()`: 計算輸入框中的運算式並顯示結果。如果運算式無效,則顯示錯誤訊息。
2. **介面佈局**:
- `entry`: 用於顯示輸入和結果的輸入框。
- `buttons`: 包含所有按鈕文字的字串列表。
- 使用巢狀迴圈來排列按鈕,並將它們添加到視窗中。
3. **主迴圈**:
- `root.mainloop()`: 啟動 Tkinter 的事件循環,使視窗保持開啟狀態。
你可以複製並執行程式碼來查看效果。希望這對你有幫助!
結果確實功能一步到位, 但版面配置我覺得有點怪, 還是 dagbs 版較優 :
不過, mannix 版的優點是, 程式碼功能註解較詳細.