01_cat.py
-----------------------------
class Cat:
def eat(self): #实例方法 self就是指最终的实例
print("猫在吃肉...")
def drink(self):
print("猫在喝可乐...")
if __name__ == "__main__":
my_cat = Cat() #实例化
my_cat.eat()
my_cat.drink()
-----------------------------
02_cat1.py
-----------------------------
class Cat:
def __init__(self,name,color):
# print("cat正在实例化")
self.cat_name = name
self.cat_color = color
self.cat_age = 3
# self.eat() #eat方法想在初始化,实例化的时候调用
def eat(self): # 实例方法 self就是指最终的实例
print("我的{}在吃肉...,他的毛是{}".format(self.cat_name,self.cat_color))
c = self.drink("牛奶")
def drink(self,drinks):
print("我的{}岁的{}在喝{}...".format(self.cat_age,self.cat_name,drinks))
alive = True
return alive
if __name__ == "__main__":
my_cat = Cat("大黄","黄色") # 实例化
print(my_cat.cat_name,my_cat.cat_age)
a = my_cat.eat()
print("a is",a)
is_alive = my_cat.drink("果粒橙")
if is_alive:
print("我的猫还活着")
else:
print("我的猫不活了")
-----------------------------
03_try1.py
-----------------------------
def add_fun(x,y):
try:
sum = x+y
print("结果是:",sum)
# except Exception as e:
# print(e)
except:
print("输入有误,请重新输入")
if __name__ == '__main__':
add_fun("123",2)
add_fun(4,2)
-----------------------------
04_student_sys_class.py
-----------------------------
class StudentSystem:
def __init__(self):
self.student_list = [{"name": "xiaohong", "age": 20, "stu_num": 10000}]
def print_info(self):
print("*" * 20)
print("欢迎来到学生信息管理系统")
print("1.展示全部学生")
print("2.搜索一个学生")
print("3.增加一个学生")
print("4.修改一个学生")
print("5.删除一个学生")
print("6.退出信息系统")
print("*" * 20)
user_input = input(">>>>请选择序号:")
return user_input
def show_all_student_info(self):
for stu in self.student_list:
print(stu)
def search_student(self):
search_student_name = input("请输入要搜索的学生姓名:")
stu_exist = False
for stu in self.student_list:
if stu["name"] == search_student_name:
stu_exist = True
print(stu)
if not stu_exist:
print(">>>您要查找的学生不存在")
def add_student_info(self):
name = input("请输入要增加的学生姓名:")
age = input("请输入要增加的学生年龄:")
stu_num = input("请输入要增加的学生学号:")
new_student = {"name": name, "age": age, "stu_num": stu_num}
self.student_list.append(new_student)
print("新的学生[{}]添加成功".format(name))
def delete_student_info(self):
name = input("请输入要删除的学生姓名:")
stu_exist = False
for stu in self.student_list:
if stu["name"] == name:
stu_exist = True
self.student_list.remove(stu)
print("您要删除的学生[{}]已经删除成功".format(name))
if not stu_exist:
print("您要删除的[{}]不存在".format(name))
def modify_student_info(self):
name = input("请输入要修改的学生姓名:")
stu_exist = False
for stu in self.student_list:
if stu["name"] == name:
stu_exist = True
stu["age"] = input("请输入修改后的年龄:")
stu["stu_num"] = input("请输入修改后的学号:")
print("您要修改的[{}]已经修改成功".format(name))
if not stu_exist:
print("您要修改的[{}]不存在".format(name))
def main(self):
while True:
# 1.提示信息,让用户输入
user_input = self.print_info()
# 2.判断用户输入
if user_input in ["1", "2", "3", "4", "5", "6"]:
# 2.1用户选择查看所有
if user_input == "1":
self.show_all_student_info()
# 2.2用户选择搜索学生
elif user_input == "2":
self.search_student()
# 2.3用户选择增加学生
elif user_input == "3":
self.add_student_info()
# 2.4用户选择删除学生
elif user_input == "5":
self.delete_student_info()
# 2.5用户选择修改学生
elif user_input == "4":
self.modify_student_info()
# 2.6选择退出
elif user_input == "6":
print("欢迎下次再来。。。")
break
else:
print("》》》输入有误,请重新选择")
if __name__ == '__main__':
studetn_system = StudentSystem()
studetn_system.main()
-----------------------------
请同学们参考练习,务必亲自敲代码,脚踏实地的编程!
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。