python学习
yimaoyingbi
这个作者很懒,什么都没留下…
展开
-
TypeError: __init__() takes 3 positional arguments but 4 were given
class Restaurant(): def __init__(self,restaurant_name,cuisine_type): self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type self.number_served = 0 def describe_restaurant(self): return(self.restaura.原创 2020-09-06 11:38:01 · 40280 阅读 · 6 评论 -
TypeError: ‘int‘ object is not callable
class Restaurant(): def __init__(self,restaurant_name,cuisine_type): self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type self.number_served = 0 def describe_restaurant(self): return(self.restaura.原创 2020-09-04 08:41:41 · 232 阅读 · 0 评论 -
#使用户输入来填充字典
responses = {}polling_active = Truewhile polling_active: #提示输入被调查者的名字和回答 name = input("\nWhat is your name") response = input("Which mountain would you like tu climb someday") #定义字典,并将答卷存在字典中 responses[name] = response #查看是否还有人要.原创 2020-08-16 21:25:49 · 655 阅读 · 0 评论 -
用while循环来处理列表和字典
for循环不能修改列表,否则将导致python难以跟踪其中的元素。要在遍历的同事对其进行修改,可还是用while;#首先创建一个待验证的用户列表,和一个用于存放已验证用户的空列表unconfirmed_users = ['alice','brain','candace']confirmed_users = []#验证每个用户,直到没有未验证用户为止;将每个经过验证的列表都移到已验证的用户列表中;while unconfirmed_users: current_user = unco.原创 2020-08-16 20:43:59 · 861 阅读 · 0 评论 -
python AttributeError: ‘list‘ object has no attribute ‘sorted‘
my_frind = ['曹大鹏','王姐','李妍']a = ['ab','fg','cg']b = a.sorted()print(b)AttributeError: 'list' object has no attribute 'sorted'sort()是方法,sorted()是函数,方法是调用,函数进行数据传递a = ['ab','fg','cg']print(sorted(a))...原创 2020-08-03 23:14:17 · 688 阅读 · 0 评论 -
TypeError: list indices must be integers or slices, not str 原因及解决方法
my_frind = ['曹大鹏','王姐','李妍']message = "hi,"+ my_frind[0] + "周末一起来吃晚饭啊"print("hi,"+ my_frind[0]+"周末一起吃饭啊")#print(message)print("王姐","没时间不能来了")del my_frind['王姐']my_frind.insert(1,"君君")print(my_frind)TypeError: list indices must be integers ...原创 2020-08-03 22:07:37 · 3800 阅读 · 0 评论 -
python的mysql数据查询及报错AttributeError: 'Connection' object has no attribute 'curson'
import pymysql#创建连接con = pymysql.connect(host='localhost',user='root',password='123456',port=3306,database='zhy')#创建游标对象cur = con.curson()#编写查询的sql语句sql = 'select * from t_student'try: cur...原创 2020-02-17 10:02:14 · 9485 阅读 · 1 评论 -
python的mysql数据库创建表及插入数据
#创建表#导入pymysqlimport pymysql#创建链接con = pymysql.connect(host='localhost',user='root',password='123456',database='zhy',port=3306)#创建游标对象cur = con.cursor()#编写创建表的sqlsql = """ create table ...原创 2020-02-17 09:15:17 · 2960 阅读 · 0 评论 -
python使用sqlite3模块删除数据报错parameters are of unsupported type
import sqlite3#链接数据库con = sqlite3.connect('C:\python_learn\DBA\SQLite3demo\sqlite3demo.db')#创建游标对象cur = con.cursor()#编写sql语句sql = "delete from t_person where pno = ?"#执行sqltry: cur.execut...原创 2020-02-15 09:58:59 · 2274 阅读 · 0 评论 -
python中sqlite3模块查询数据一条或多条
#导入模块import sqlite3#创建链接con = sqlite3.connect('C:\python_learn\DBA\SQLite3demo\sqlite3demo.db')#创建游标对象cur = con.cursor()#编写sql语句sql = "select * from t_person "#执行语句try: cur.execute(sql)...原创 2020-02-15 09:27:44 · 3024 阅读 · 0 评论 -
python数据库表sqlite3模块插入数据
import sqlite3#连接数据库con = sqlite3.connect('C:\python_learn\DBA\SQLite3demo\sqlite3demo.db')#创建游标cur = con.cursor()#编写sql语句sql = 'insert into t_person(pname,age) values(?,?)' #括号中是插入的字段,用逗号隔...原创 2020-02-14 12:53:49 · 1982 阅读 · 0 评论 -
python如何使用sqlite3模块创建数据库表
'''1.导入sqlite3模块2.创建连接sqlite3.connect()3.创建游标对象4.编写创建表的sql的语句5.执行sql(包括异常语句try)6.关闭连接'''import sqlite3#创建连接con = sqlite3.connect('C:\python_learn\DBA\SQLite3demo\sqlite3demo.db')print(con...原创 2020-02-14 12:20:23 · 2971 阅读 · 0 评论 -
pygame模块在pycharm的安装
一、进入pycharm,在file中找到"Settings"点击进入;二、找到Project Interpreter 选项:右侧为已安装的模块。点击最右侧的“+”新增模块三、搜索Pygame点击左下角的安装按钮【install Package】进行安装;...原创 2020-02-14 09:58:21 · 272 阅读 · 1 评论 -
python使用递归计算n的阶乘(s!=5*4*3*2*1)
#coding= utf-8#使用递归计算n的阶乘(s!=5*4*3*2*1)def factorial(a): if n==1: return n else: return n*factorial(n-1)print(factorial(5))#打印结果120原创 2020-02-08 22:13:04 · 3975 阅读 · 0 评论 -
python读写csv文件,使用csv.reader()方法及csv.writer()方法
python读写csv文件,使用csv.reader()方法及csv.writer()方法import csvwith open(r"C:\郑宏宇\python_learn\c.csv","r") as f: a_csv = csv.reader(f) for row in a_csv: print(row)with open(r"C:\郑宏宇\pyt...原创 2020-02-08 08:06:34 · 7108 阅读 · 0 评论 -
python使用pickle实现序列化和反序列化
#使用pickle实现序列和反序列化import picklea1 = "中国"a2 = 12345a3 = [10,20,30,40,50]with open(r"C:\郑宏宇\python_learn\t.txt","wb")as f: pickle.dump(a1,f) pickle.dump(a2,f) pickle.dump(a3,f)with...原创 2020-02-08 07:17:49 · 313 阅读 · 0 评论 -
python读文件集中方式以及编码错误UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 12: illegal mu
s = ["您好\n","非常好\n","很高兴认识你\n"]with open(r"C:\郑宏宇\python_learn\t.txt","w",encoding = "utf-8") as f: f.writelines(s)#读取一个文件前4个字符with open(r"C:\郑宏宇\python_learn\t.txt","r",encoding = "utf-8") ...原创 2020-02-06 22:01:03 · 266 阅读 · 0 评论 -
python类组合
class A2: def say_a2(self): print("a2,a2,a2")class B2: def __init__(self,a): self.a = aa2 = A2()b2 = B2(a2)b2.a.say_a2()采用组合的方法同样可以实现类继承;...原创 2020-02-03 16:37:08 · 119 阅读 · 0 评论 -
python多态及isinstance
class study(): def eat(self): print("饿了,吃饭了")class Chinese(Man): def study(self): print("学习汉语")class English(): def study(self): print("学习英语")class Indian(Man...原创 2020-02-03 12:40:28 · 172 阅读 · 0 评论 -
python构造函数
class Employee: def __init__(self,name,age): self.name = name self.__age = age print("您好") def __work(self): print("疫情严重,在家学习") print("年龄:{0}".format(...原创 2020-02-03 08:36:28 · 312 阅读 · 0 评论 -
python,私有属性运行报错; object has no attribute
python对于类的成员没有严格的访问控制限制,这与其他面向对象的语言有区别。关于私有属性和私有方法,有如下要点:1)通常我们约定,两个下划线开头的属性是私有的,其他是公共的;2)类内部可以访问私有属性(方法)3)类外部不能直接访问私有属性(方法)4)类外部可以通过“_雷鸣__私有属性(方法)”访问私有属性(方法)class Employee: def __init__(s...原创 2020-02-01 17:15:55 · 84440 阅读 · 4 评论 -
python构造函数__init__提示:Student() takes no arguments
class Student: def _init_(self,name,score): #应该是__init__ self.name = name self.score = score def say_score(self): print("{0}的分数是:{1}".format(self.name,self.score))...原创 2020-01-31 19:18:58 · 508 阅读 · 0 评论