添加用户后报错

添加用户后报错

还请老

                elif opt == "2":
                    while True:
                        os.system("cls")
                        print(Fore.LIGHTGREEN_EX, "\n\t1.添加用户")
                        print(Fore.LIGHTGREEN_EX, "\n\t2.修改用户")
                        print(Fore.LIGHTGREEN_EX, "\n\t3.删除用户")
                        print(Fore.LIGHTRED_EX, "\n\tback.返回上一层")
                        print(Style.RESET_ALL)
                        opt = input("\n\t输入操作编号:")
                        if opt == "back":
                            break
                        elif opt == "1":
                            os.system("cls")
                            username = input("\n\t用户名:")
                            password = getpass("\n\t密码:")
                            repassword = getpass("\n\t重复密码:")
                            if password != repassword:
                                print("\n\t两次密码不一致(3秒自动返回)")
                                time.sleep(3)
                                continue
                            email = input("\n\t邮箱:")
                            result = __role_service.search_list()
                            for index in range(len(result)):
                                one = result[index]
                                print(Fore.LIGHTBLUE_EX, "\n\t%d.%s" % (index + 1, one[1]))
                            print(Style.RESET_ALL)
                            opt = input("\n\t角色编号:")
                            role_id = result[int(opt) - 1][0]
                            __user_service.insert(username, password, email, role_id)
                            print("\n\t保存成功(3秒自动返回)")
                            time.sleep(3)
                elif opt=="back":
                    break
                elif opt=="exit":
                    sys.exit(0)
    else:
        print("\n\t登录失败(3秒自动返回)")
        time.sleep(3)
elif opt=="2":
    sys.exit(0)

师指导下https://img1.sycdn.imooc.com//climg/617a788609ceba0a13680490.jpg

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

3回答
好帮手慕美 2021-10-28 18:26:32

同学,你好!运行同学的代码没有报错,同学将app.py中的代码及项目的目录结构提供下

https://img1.sycdn.imooc.com//climg/617a7ac109ec4adc01840430.jpg

祝学习愉快!

  • 提问者 慕村8230135 #1
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    from  colorama import  Fore,Style #字体颜色\重置字体颜色
    from  getpass import getpass #遮挡用户密码
    from service.user_service import UserService
    from service.news_service import NewsService
    from service.role_service import RoleService
    
    import os
    import sys
    import time
    __user_service=UserService()
    __news_service=NewsService()
    __role_service=RoleService
    while True:
        os.system("cls")
        print(Fore.LIGHTBLUE_EX,"\n\t========")
        print(Fore.LIGHTBLUE_EX,"\n\t欢迎使用新闻系统")
        print(Fore.LIGHTBLUE_EX,"\n\t========")
        print(Fore.LIGHTRED_EX,"\n\t1.登录系统")
        print(Fore.LIGHTRED_EX,"\n\t2.退出系统")
        print(Style.RESET_ALL)#重置字体颜色
        opt=input("\n\t输入操作编号:")
        if opt=="1":
            username=input("\n\t用户名:")
            password=getpass("\n\t密码:")
            result=__user_service.login(username,password)
            #登录成功
            if result==True:
                #查询角色
                role=__user_service.search_user_role(username)
                while True:
                    os.system("cls")
                    if role=="新闻编辑":
                        print("test")
                    elif role=="管理员":
                        print(Fore.LIGHTBLACK_EX,"\n\t1.新闻管理")
                        print(Fore.LIGHTBLACK_EX,"\n\t2.用户管理")
                        print(Fore.LIGHTBLACK_EX,"\n\tback.退出登录")
                        print(Fore.LIGHTBLACK_EX,"\n\texit.退出系统")
                        print(Style.RESET_ALL)  # 重置字体颜色
                        opt = input("\n\t输入操作编号:")
                        if opt == "1":
                            while True:
                                os.system("cls")
                                print(Fore.LIGHTBLACK_EX, "\n\t1.审批新闻")
                                print(Fore.LIGHTBLACK_EX, "\n\t2.删除新闻")
                                print(Fore.LIGHTBLACK_EX, "\n\tback.返回上一层")
                                print(Style.RESET_ALL)  # 重置字体颜色
                                opt = input("\n\t输入操作编号:")
                                if opt == "1":
                                    page = 1
                                    while True:
                                        os.system("cls")
                                        count_page = __news_service.search_unreview_count_page()
                                        result = __news_service.search_unreview_list(page)
                                        for index in range(len(result)):
                                            one = result[index]
                                            print(Fore.LIGHTCYAN_EX,
                                                  "\n\t%d\t%s\t%s\t%s" % (index + 1, one[1], one[2], one[3]))
                                        print(Fore.LIGHTBLUE_EX, "\n\t-----------------------------")
                                        print(Fore.LIGHTBLUE_EX, "\n\t%d/%d" % (page, count_page))
                                        print(Fore.LIGHTBLUE_EX, "\n\t----------------------------")
                                        print(Fore.LIGHTRED_EX, "\n\tback.返回上一层")
                                        print(Fore.LIGHTRED_EX, "\n\tprev.上一页")
                                        print(Fore.LIGHTRED_EX, "\n\tnext.下一页")
                                        print(Style.RESET_ALL)  # 重置字体颜色
                                        opt = input("\n\t输入操作编号:")
                                        if opt=="back":
                                            break
                                        elif opt=="prev" and page>1:
                                            page-=1
                                        elif opt=="next" and page<count_page:
                                            page+=1
                                        elif int(opt)>=1 and int(opt)<=10:
                                            news_id=result[int(opt)-1][0]
                                            __news_service.update_unreview_news(news_id)
                                elif opt == "2":
                                    page = 1
                                    while True:
                                        os.system("cls")
                                        count_page = __news_service.search_count_page()
                                        result = __news_service.search_list(page)
                                        for index in range(len(result)):
                                            one = result[index]
                                            print(Fore.LIGHTCYAN_EX,
                                                  "\n\t%d\t%s\t%s\t%s" % (index + 1, one[1], one[2], one[3]))
                                        print(Fore.LIGHTBLUE_EX, "\n\t-----------------------------")
                                        print(Fore.LIGHTBLUE_EX, "\n\t%d/%d" % (page, count_page))
                                        print(Fore.LIGHTBLUE_EX, "\n\t----------------------------")
                                        print(Fore.LIGHTRED_EX, "\n\tback.返回上一层")
                                        print(Fore.LIGHTRED_EX, "\n\tprev.上一页")
                                        print(Fore.LIGHTRED_EX, "\n\tnext.下一页")
                                        print(Style.RESET_ALL)  # 重置字体颜色
                                        opt = input("\n\t输入操作编号:")
                                        if opt=="back":
                                            break
                                        elif opt=="prev" and page>1:
                                            page-=1
                                        elif opt=="next" and page<count_page:
                                            page+=1
                                        elif int(opt)>=1 and int(opt)<=10:
                                            news_id=result[int(opt)-1][0]
                                            __news_service.delete_by_id(news_id)
                        elif opt == "2":
                            while True:
                                os.system("cls")
                                print(Fore.LIGHTGREEN_EX, "\n\t1.添加用户")
                                print(Fore.LIGHTGREEN_EX, "\n\t2.修改用户")
                                print(Fore.LIGHTGREEN_EX, "\n\t3.删除用户")
                                print(Fore.LIGHTRED_EX, "\n\tback.返回上一层")
                                print(Style.RESET_ALL)
                                opt = input("\n\t输入操作编号:")
                                if opt == "back":
                                    break
                                elif opt == "1":
                                    os.system("cls")
                                    username = input("\n\t用户名:")
                                    password = getpass("\n\t密码:")
                                    repassword = getpass("\n\t重复密码:")
                                    if password != repassword:
                                        print("\n\t两次密码不一致(3秒自动返回)")
                                        time.sleep(3)
                                        continue
                                    email = input("\n\t邮箱:")
                                    result = __role_service.search_list()
                                    for index in range(len(result)):
                                        one = result[index]
                                        print(Fore.LIGHTBLUE_EX, "\n\t%d.%s" % (index + 1, one[1]))
                                    print(Style.RESET_ALL)
                                    opt = input("\n\t角色编号:")
                                    role_id = result[int(opt) - 1][0]
                                    __user_service.insert(username, password, email, role_id)
                                    print("\n\t保存成功(3秒自动返回)")
                                    time.sleep(3)
                        elif opt=="back":
                            break
                        elif opt=="exit":
                            sys.exit(0)
            else:
                print("\n\t登录失败(3秒自动返回)")
                time.sleep(3)
        elif opt=="2":
            sys.exit(0)


    2021-10-28 20:59:49
  • 提问者 慕村8230135 #2

    https://img1.sycdn.imooc.com//climg/617a9f0e09bf02fc14890988.jpg

    2021-10-28 21:01:09
慕村8230135 提问者 2021-10-28 18:18:34
from db.role_dao import RoleDao


class RoleService:
    __role_dao=RoleDao()

    #查询角色列表
    def search_list(self):
        result=self.__role_dao.search_list()
        return result


慕村8230135 提问者 2021-10-28 18:18:08
from db.mysql_db import pool

class RoleDao:
    #查询角色列表
    def search_list(self):
        try:
            con = pool.get_connection()  # 获取数据库链接
            cursor = con.cursor()  # 创建游标
            sql = "select id,role from t_role"
            cursor.execute(sql)
            result = cursor.fetchall()
            return result
        except Exception as e:
            print(e)
        finally:
            if "con" in dir():
                con.close()


  • 同学,你好!

    RoleService类对象实例化时未加括号,导致没有实例对象而报错,如下图:RoleService类实例化时要加( )

    https://img1.sycdn.imooc.com//climg/617b68a509d5cd8d04020139.jpg

    祝学习愉快~

    2021-10-29 11:25:54
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师