会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133561个问题
JAVA 全系列/第四阶段:数据库与AI协同技术实战/SQL 语言 16351楼
软件测试 全系列/第十阶段:接口自动化测试/使用Postman测试接口 16352楼
Python 全系列/第五阶段:Linux 环境编程基础/ShellScript脚本编程 16353楼
Python 全系列/第一阶段:Python入门/控制语句 16354楼
JAVA 全系列/第二十一阶段:租房网(Spring Cloud最新架构)/租房网 16355楼

#实现数据的输入
lists = []
while True:
    a = input("请输入学生的学号(a或A结束输入):")
    if a.upper() == "A":
        print("结束输入")
        break
    b = input("请输入学生的姓名:")
    c = input("请输入学生的成绩:")
    list = [a+",",b+",",c]
    list.append("\n")
    lists.extend(list)

    with open("ts_file","a") as f:
        for i in lists:
            f.writelines(i)

    print()

#定义一个异常
class NameError(Exception):
    def __init__(self,name):
        Exception.__init__(self)
        self.name = name
    def __str__(self):
        return "名字输入错误"

print()
print("开始使用")

#实现数据的采用
with open("ts_file","r") as f:
    a = f.readlines()
    student_number = input("请输入学生的学号:")
    student_name = input("请输入学生的姓名:")
    try:
        for i in a:
             newlist=i.split(",")
            if newlist[0] == student_number and newlist[1] == student_name:
                print("成绩是:{}".format(newlist[2]))
            else:
                if newlist[1] != student_name:
                    raise NameError(student_name)
    except BaseException as var:
        print(var)

老师,后面这个为什么没有办法输出呢,我看了很多遍一直觉得没啥问题

Python 全系列/第二阶段:Python 深入与提高/文件处理 16357楼
Python 全系列/第十六阶段:Python 爬虫开发/爬虫基础 16358楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Spring 16359楼
JAVA 全系列/第一阶段:AI驱动的JAVA编程/面向对象详解和JVM底层内存分析 16360楼
JAVA 全系列/(旧的隐藏)第二十一阶段:百战商城项目(Spring Cloud最新架构)/百战商城项目 16361楼
Python 全系列/第四阶段:函数式编程和核心特性/python新特性 16362楼

老师 我代码跟着视频一样的 却老是结果不对?

image.png

#dbUtil.py
import pymysql
class Mydb:
    config={
        "host":"localhost",
        "user":"root",
        "password":"root",
        "db":"music_project",
        "charset":"utf8"
    }
    def __init__(self):
        self.connection=pymysql.connect(**Mydb.config)
        self.cursor=self.connection.cursor()
    def close(self):
        if self.cursor:
            self.cursor.close()
        if self.connection:
            self.connection.close()

    def exeDML(self,sql,*args):
        try:
         
            count=self.cursor.execute(sql,args)
            
            self.connection.commit()
            return count
        except Exception as e:
            print(e)
            if self.connection:
                self.connection.rollback()
        finally:
            self.close()
    def query_one(self,sql,*args):
        try:
            self.cursor.execute(sql,args)
            self.cursor.fetchone()
        except Exception as e:
            print(e)
        finally:
            self.close()
    def query_all(self,sql,*args):
        try:
     
            self.cursor.execute(sql,args)
    
            return self.cursor.fetchall()
        except Exception as e:
            print(e)
        finally:
            self.close()

if __name__=='__main__':
    dbutil=Mydb()
    
 #play.py
from MyService import MyService
if __name__=="__main__":
    uname=input("请输入用户名:")
    password=input("请输入密码:")
    myservice=MyService()
    if myservice.login(uname,password):
        print("登陆成功")
    else:
        print("登陆失败")
#MyService.py
from dbUtil import Mydb
class MyService:
    def login(self,uname,password):
        sql="select * from t_user where uname=%s and password=%s"
        user=Mydb().query_one(sql,uname,password)
        if user:
            return True
        else:
            return False



Python 全系列/第六阶段:数据库与AI协同技术实战/项目-音乐播放器-旧 16365楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637