weixin_58310069 2021-08-30 20:43 采纳率: 33.3%
浏览 35
已结题

以下Python代码运行报错,请求帮助


def do_():
    import time, sys, pyautogui, os, bs4, requests
    a = '登陆成功,'
    password = pyautogui.password('请输入密码:')
    if password == '134679':
        global mode
        mode = pyautogui.confirm(a + '请选择模式', buttons=['1.创建文件', '2.删除文件', '3.运行程序', '4.翻译'])
def do():
    import time,sys,pyautogui,os,bs4,requests
    if open('f.txt','r').read()=='1':

        mode = pyautogui.confirm('请选择模式', buttons=['1.创建文件', '2.删除文件', '3.运行程序', '4.翻译'])
        if mode == '1.创建文件':
            where = pyautogui.prompt('请输入路径(绝对路径或相对路径):')
            where = where.replace('\\', '/')
            f = open(where, 'w')
            x = pyautogui.confirm('是否写入文本?', buttons=['是', '否'])
            if x == '是':
                f.write(pyautogui.prompt('请输入要写入的文本'))
            f.close()
            x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
            if x == '是':
                f = open('f.txt', 'w+')
                f.write('1')
                f.close()
                do()
        elif mode == '2.删除文件':
            try:

                where = pyautogui.prompt('请输入文件路径(绝对路径或相对路径):')
                where = where.replace('\\', '/')
                os.remove(where)
                x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
                if x == '是':
                    f = open('f.txt', 'w+').write('1')
                    f.write('1')
                    f.close()
                    do()
            except FileNotFoundError:
                pyautogui.alert('找不到文件!')
                x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
                if x == '是':
                    f = open('f.txt', 'w+').write('1')
                    f.write('1')
                    f.close()
                    do()
        elif mode == '3.运行程序':
            try:
                name = pyautogui.prompt('请输入程序路径:')
                os.system(name)
                x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
                if x == '是':
                    f = open('f.txt', 'w+').write('1')
                    f.write('1')
                    f.close()
                    do()
            except FileNotFoundError:
                pyautogui.alert('找不到程序!')
                x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
                if x == '是':
                    f = open('f.txt', 'w+').write('1')
                    f.write('1')
                    f.close()
                    do()
        elif mode == '4.翻译':
            import requests, sys, bs4, time
            words = pyautogui.prompt('请输入要翻译的英文:')
            head = {
                "User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11",
            }
            try:
                url = "http://www.youdao.com/w/" + words + "/#keyfrom=dict2.top"
                time.sleep(1)
                response = requests.get(url, headers=head)
                soup = bs4.BeautifulSoup(response.text, "lxml")
                data = soup.find(name="div", class_="trans-container").find_all(name="li")
                y = ''
                for n in data:
                    y += n.text
                pyautogui.alert('\n单词 '+ words +' 的意思是:'+y)
                x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
                if x == '是':
                    f = open('f.txt', 'w+').write('1')
                    f.write('1')
                    f.close()
                    do()
            except:
                pyautogui.alert("查询出错啦!")
                x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
                if x == '是':
                    f = open('f.txt', 'w+').write('1')
                    f.write('1')
                    f.close()
                    do()

        else:
            pyautogui.alert('密码错误!五秒后退出系统!')
            time.sleep(5)
            sys.exit()

    else:
        do_()
        if mode == '1.创建文件':
            where = pyautogui.prompt('请输入路径(绝对路径或相对路径):')
            where = where.replace('\\', '/')
            f = open(where, 'w')
            x = pyautogui.confirm('是否写入文本?', buttons=['是', '否'])
            if x == '是':
                f.write(pyautogui.prompt('请输入要写入的文本'))
            f.close()
            x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
            if x == '是':
                f = open('f.txt', 'w+')
                f.write('1')
                f.close()
                do()
        elif mode == '2.删除文件':
            try:

                where = pyautogui.prompt('请输入文件路径(绝对路径或相对路径):')
                where = where.replace('\\', '/')
                os.remove(where)
                x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
                if x == '是':
                    f = open('f.txt', 'w+').write('1')
                    f.write('1')
                    f.close()
                    do()
            except FileNotFoundError:
                pyautogui.alert('找不到文件!')
                x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
                if x == '是':
                    f = open('f.txt', 'w+').write('1')
                    f.write('1')
                    f.close()
                    do()
        elif mode == '3.运行程序':
            try:
                name = pyautogui.prompt('请输入程序路径:')
                os.system(name)
                x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
                if x == '是':
                    f = open('f.txt', 'w+').write('1')
                    f.write('1')
                    f.close()
                    do()
            except FileNotFoundError:
                pyautogui.alert('找不到程序!')
                x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
                if x == '是':
                    f = open('f.txt', 'w+').write('1')
                    f.write('1')
                    f.close()
                    do()
        elif mode == '4.翻译':
            import requests, sys, bs4, time
            words = pyautogui.prompt('请输入要翻译的英文:')
            head = {
                "User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11",
            }
            try:
                url = "http://www.youdao.com/w/" + words + "/#keyfrom=dict2.top"
                time.sleep(1)
                response = requests.get(url, headers=head)
                soup = bs4.BeautifulSoup(response.text, "lxml")
                data = soup.find(name="div", class_="trans-container").find_all(name="li")
                y=''
                for n in data:
                    y+=n.text
                pyautogui.alert('\n单词 '+ words +' 的意思是:'+y)
                x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
                if x == '是':
                    f = open('f.txt', 'w+').write('1')
                    f.write('1')
                    f.close()
                    do()
            except:
                pyautogui.alert("查询出错啦!")
                x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
                if x == '是':
                    f = open('f.txt', 'w+').write('1')
                    f.write('1')
                    f.close()
                    do()


do()
with open('f.txt','w') as z:
    z.write('')

报错提示:Traceback (most recent call last):
File "E:\python\操作.py", line 188, in
do()
File "E:\python\操作.py", line 104, in do
if mode == '1.创建文件':
UnboundLocalError: local variable 'mode' referenced before assignment

  • 写回答

3条回答 默认 最新

  • Pliosauroidea 2021-08-30 20:53
    关注

    你的mode是在第11行声明的,104行和11行处于一组互吃的if-else中,所以对于104行来说,mode是未被定义的,所以会报错
    有帮助望采纳

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 9月9日
  • 已采纳回答 9月1日
  • 修改了问题 8月30日
  • 创建了问题 8月30日

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制