You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python消息编解码开发出现ZeroDivisionError除零错误如何解决

错误原因
  • 触发ZeroDivisionError的直接原因是未输入私钥就点击了执行按钮:此时private_key.get()返回空字符串,len(key)计算结果为0,执行i % len(key)运算时触发除零错误。
  • 除此之外代码还存在多处语法/逻辑错误,会导致后续运行报错:
    1. 模式判断逻辑错误:if(mode.get() == 'e' or 'E')的写法不符合Python规则,or 'E'永远为真,无论输入什么模式都会走编码分支;
    2. 方法名拼写错误:Encode函数中base64.urlsafe_bs64encode拼写错误,正确写法是base64.urlsafe_b64encode;
    3. 括号位置错误:Decode函数中ord(message[i]- ord(key_c))括号写反,应该是ord(message[i]) - ord(key_c),否则会尝试对字符串做减法运算触发类型错误;
    4. 结果赋值错误:Mode函数的else分支用了Result.get,应该改为Result.set才能把错误提示写入结果框。
修复代码
from tkinter import *
import base64

root = Tk()

root.geometry('500x300')
root.resizable(0,0)
root.title("Message Encode and Decode")

Label(root, text='ENCODE DECODE', font = 'arial 20 bold').pack()

Label(root, text= 'ZussM4n', font = 'arial 20 bold').pack(side = BOTTOM)

#defining variables
Text = StringVar()
private_key = StringVar()
mode = StringVar()
Result = StringVar()

#algoritmo encoder
def Encode(key, message):
    enc=[]
    for i in range(len(message)):
        key_c = key[i % len(key)]
        enc.append(chr((ord(message[i]) + ord(key_c)) % 256))
    # 修复base64方法名拼写错误
    return base64.urlsafe_b64encode("".join(enc).encode()).decode()

#algoritmno decoder
def Decode(key, message):
    dec=[]
    message = base64.urlsafe_b64decode(message).decode()
    for i in range(len(message)):
        key_c = key[i % len(key)]
        # 修复括号位置错误
        dec.append(chr((256 + ord(message[i]) - ord(key_c)) % 256))
    return "".join(dec)

#function to set mode
def Mode():
    # 新增输入校验,避免除零错误
    input_key = private_key.get().strip()
    input_msg = Text.get().strip()
    input_mode = mode.get().strip().lower()
    if not input_key:
        Result.set("错误:私钥不能为空")
        return
    if not input_msg:
        Result.set("错误:消息内容不能为空")
        return
    if input_mode == 'e':
        Result.set(Encode(input_key, input_msg))
    elif input_mode == 'd':
        Result.set(Decode(input_key, input_msg))
    else:
        # 修复get改为set
        Result.set('错误:模式仅支持e(编码)/d(解码)')

#Exit
def Exit():
    root.destroy()

#Reset window
def Reset():
    Text.set("")
    private_key.set("")
    mode.set("")
    Result.set("")

#Labels and Buttons

Label(root, font= 'arial 12 bold', text='MESSAGE').place(x= 60,y=60)
Entry(root, font = 'arial 10', textvariable = Text, bg = 'ghost white').place(x=290, y = 60)

Label(root, font = 'arial 12 bold', text ='KEY').place(x=60, y = 90)
Entry(root, font = 'arial 10', textvariable = private_key , bg ='ghost white').place(x=290, y = 90)

Label(root, font = 'arial 12 bold', text ='MODE(e-encode, d-decode)').place(x=60, y = 120)
Entry(root, font = 'arial 10', textvariable = mode , bg= 'ghost white').place(x=290, y = 120)
Entry(root, font = 'arial 10 bold', textvariable = Result, bg ='ghost white').place(x=290, y = 150)

Button(root, font = 'arial 10 bold', text = 'RESULT'  ,padx =2,bg ='LightGray' ,command = Mode).place(x=60, y = 150)

Button(root, font = 'arial 10 bold' ,text ='RESET' ,width =6, command = Reset,bg = 'LimeGreen', padx=2).place(x=80, y = 190)

Button(root, font = 'arial 10 bold',text= 'EXIT' , width = 6, command = Exit,bg = 'OrangeRed', padx=2, pady=2).place(x=180, y = 190)

root.mainloop()
修复说明

新增了输入校验逻辑,私钥、消息为空或者模式输入错误时,会直接在结果框显示错误提示,不会进入编解码逻辑触发崩溃。同时修复了所有语法、逻辑错误,可正常运行。

内容的提问来源于stack exchange,提问作者ZussM4n

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 22:06:04