python基础小练习
def main():
number = int(input("请输入学生的总人数:")) # 输入要录入的学生总数
count = number # 用一个变量来保存这个学生总数
students = list() # 创建一个列表来存储学生信息
while(number): # 进入循环
student = {} # 学生信息列表
name = input("请输入学生的姓名:") # 输入学生姓名
student['name'] = name # 字典赋值
gender = input("请输入他的性别:") # 输入性别
student['gender'] = gender # 字典赋值
students.append(student) # 将这个信息新增到列表
number -= 1 # 学生总数-1
print("%d 个学生信息是==========> %s"%(count, students)) # 循环结束,打印列表信息 if __name__ == '__main__':
main()
# 学生信息录入展示代码
# 输入要录入的学生总数
# 录入学生基本信息
# 学生信息录入完毕展示给用户
import os
import re
import numpy as np class Student: #定义一个学生类
def __init__(self):
self.name = ''
self.ID =''
self.score1 = 0
self.score2 = 0
self.score1 = 0
self.sum = 0 def searchByID(stulist, ID): #按学号查找看是否学号已经存在
for item in stulist:
if item.ID == ID:
return True def Add(stulist,stu): #添加一个学生信息
if searchByID(stulist, stu.ID) == True:
print("学号已经存在!")
return False
stulist.append(stu)
print( stu.name,stu.ID, stu.score1, stu.score2, stu.score3, stu.sum)
print( "是否要保存学生信息?")
nChoose = input("Choose Y/N")
if nChoose == 'Y' or nChoose == 'y':
file_object = open("students.txt", "a")
file_object.write(stu.ID)
file_object.write(" ")
file_object.write(stu.name)
file_object.write(" ")
file_object.write(str(stu.score1))
file_object.write(" ")
file_object.write(str(stu.score2))
file_object.write(" ")
file_object.write(str(stu.score3))
file_object.write(" ")
file_object.write(str(stu.sum))
file_object.write("\n")
file_object.close()
print( u"保存成功!") def Search(stulist, ID): #搜索一个学生信息
print( "学号 姓名 语文 数学 英语 总分")
count = 0
for item in stulist:
if item.ID == ID:
print( item.ID, '\t' ,item.name,'\t', item.score1,'\t',item.score2, '\t', item.score3, '\t',item.sum)
break
count = 0
if count == len(stulist):
print( "没有该学生学号!") def Del(stulist, ID): #删除一个学生信息
count = 0
for item in stulist:
if item.ID == ID:
stulist.remove(item)
print( "删除成功!")
break
count +=1
# if count == len(stulist):
# print( "没有该学生学号!"
file_object = open("students.txt", "w")
for stu in stulist:
print( stu.ID, stu.name, stu.score1,stu.score2, stu.score3, stu.sum)
file_object.write(stu.ID)
file_object.write(" ")
file_object.write(stu.name)
file_object.write(" ")
file_object.write(str(stu.score1))
file_object.write(" ")
file_object.write(str(stu.score2))
file_object.write(" ")
file_object.write(str(stu.score3))
file_object.write(" ")
file_object.write(str(stu.sum))
file_object.write("\n")
file_object.close()
# print( "保存成功!"
file_object.close()
def Change(stulist, ID):
count = 0
for item in stulist:
if item.ID == ID:
stulist.remove(item)
file_object = open("students.txt", "w")
for stu in stulist:
#print( li.ID, li.name, li.score
file_object.write(stu.ID)
file_object.write(" ")
file_object.write(stu.name)
file_object.write(" ")
file_object.write(str(stu.score1))
file_object.write(" ")
file_object.write(str(stu.score2))
file_object.write(" ")
file_object.write(str(stu.score3))
file_object.write(" ")
file_object.write(str(stu.sum))
file_object.write("\n")
# print( "保存成功!"
file_object.close()
stu = Student()
stu.name = input("请输入学生的姓名")
while True:
stu.ID = input("请输入学生的ID")
p = re.compile('^[0-9]{3}$')
if p.match(stu.ID):
break
else:
print( "输入的有错误!")
while True:
stu.score1 = int(input("请输入学生语文成绩"))
if stu.score1 <= 100 and stu.score1 > 0 :
break
else:
print( "输入的学生成绩有错误!")
while True:
stu.score2 = int(input("请输入学生数学成绩"))
if stu.score2 <= 100 and stu.score2 > 0 :
break
else:
print( "输入的学生成绩有错误!")
while True:
stu.score3 = int(input("请输入学生英语成绩"))
if stu.score3 <= 100 and stu.score3 > 0 :
break
else:
print( "输入的学生成绩有错误!")
stu.sum = stu.score1 + stu.score2 + stu.score3
Add(stulist,stu)
def display(stulist): #显示所有学生信息
print( u"学号 姓名 语文 数学 英语 总分")
for item in stulist:
print( item.ID, '\t' ,item.name,'\t', item.score1,'\t',item.score2, '\t', item.score3, '\t',item.sum) def Sort(stulist): #按学生成绩排序
Stu = []
sum_count = []
for li in stulist:
temp = []
temp.append(li.ID)
temp.append(li.name)
temp.append(int(li.score1))
temp.append(int(li.score2))
temp.append(int(li.score3))
temp.append(int(li.sum))
sum_count.append(int(li.sum))
Stu.append(temp) #print( sum_count
#print( Stu;
#print( stulist
insertSort(sum_count, stulist)
#print( stulist;
display(stulist) def insertSort(a, stulist):
for i in range(len(a)-1):
#print( a,i
for j in range(i+1,len(a)):
if a[i]<a[j]:
temp = stulist[i]
stulist[i] = stulist[j]
stulist[j] = temp
#return a def Init(stulist): #初始化函数
print( "初始化......")
file_object = open('students.txt', 'r')
for line in file_object:
stu = Student()
line = line.strip("\n")
s = line.split(" ")
stu.ID = s[0]
stu.name = s[1]
stu.score1 = s[2]
stu.score2 = s[3]
stu.score3 = s[4]
stu.sum = s[5]
stulist.append(stu)
file_object.close()
print( "初始化成功!")
main() def main(): #主函数 该程序的入口函数
while True:
print( "*********************")
print( u"--------菜单---------")
print( u"增加学生信息--------1")
print( u"查找学生信息--------2")
print( u"删除学生信息--------3")
print( u"修改学生信息--------4")
print( u"所有学生信息--------5")
print( u"按照分数排序--------6")
print( u"退出程序------------0")
print( "*********************") nChoose = input("请输入你的选择:")
if nChoose == "":
stu = Student()
stu.name = input("请输入学生的姓名")
while True:
stu.ID = input("请输入学生的ID")
p = re.compile('^[0-9]{3}$')
if p.match(stu.ID):
break
else:
print( "输入的有错误!")
while True:
stu.score1 = int(input("请输入学生语文成绩"))
if stu.score1 <= 100 and stu.score1 > 0 :
break
else:
print( "输入的学生成绩有错误!")
while True:
stu.score2 = int(input("请输入学生数学成绩"))
if stu.score2 <= 100 and stu.score2 > 0 :
break
else:
print( "输入的学生成绩有错误!")
while True:
stu.score3 = int(input("请输入学生英语成绩"))
if stu.score3 <= 100 and stu.score3 > 0 :
break
else:
print( "输入的学生成绩有错误!")
stu.sum = stu.score1 + stu.score2 + stu.score3
Add(stulist,stu) if nChoose == '':
ID = input("请输入学生的ID")
Search(stulist, ID) if nChoose == '':
ID = input("请输入学生的ID")
Del(stulist, ID)
if nChoose == '':
ID = input("请输入学生的ID")
Change(stulist, ID) if nChoose == '':
display(stulist) if nChoose == '':
Sort(stulist) if nChoose == '':
break if __name__ == '__main__':
stulist =[]
Init(stulist)
python基础小练习的更多相关文章
- python基础小知识,is和==的区别,编码和解码
1.is和==的区别 1)id() 通过id()我们可以查看到一个变量表示的值在内存中的地址 >>> s1 = "Tanxu" >>> s2 = ...
- python基础--小数据池,代码块的最详细、深入剖析
本文转至太白金星 一,id,is,== 在Python中,id是什么?id是内存地址,那就有人问了,什么是内存地址呢? 你只要创建一个数据(对象)那么都会在内存中开辟一个空间,将这个数据临时加在到内存 ...
- python基础-小练习
三级菜单 要求: 打印省.市.县三级菜单 可返回上一级 可随时退出程序 购物车程序 要求: 用户名和密码存放于文件中,格式为:egon|egon123 启动程序后,先登录,登录成功则让用户输入工资,然 ...
- python基础小点
变量的命名规则 由字母.下划线.数字组成,且不能以数字开头 不能用关键字作为变量名 最好不要与python内置的一些方法和类名冲突 变量名应尽量简短且具有意义,多个单词之间用下划线连接 注释 # - ...
- Python基础-小程序练习(跳出多层循环,购物车,多级菜单,用户登录)
一. 从第3层循环直接跳出所有循环 break_flag = False count = 0 while break_flag == False: print("-第一层") wh ...
- python基础之 数据类型的补充,小数据类型
1.id is详解 ID 在Python中,id是什么?id是内存地址,比如你利用id()内置函数去查询一个数据的内存地址: name = 'nba' print(id(name)) # 158583 ...
- Python基础知识(六)------小数据池,集合,深浅拷贝
Python基础知识(六)------小数据池,集合,深浅拷贝 一丶小数据池 什么是小数据池: 小数据池就是python中一种提高效率的方式,固定数据类型使用同一个内存地址 代码块 : 一个文 ...
- python基础练习-猜年龄、编写登陆接口小程序
python基础练习: 一.猜年龄 , 可以让用户最多猜三次! age=40 count = 1 while count <=3 : user_guess=int(input("i ...
- Python开发【第二篇】:Python基础知识
Python基础知识 一.初识基本数据类型 类型: int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647 在64位 ...
随机推荐
- luogu2604 [ZJOI2010]网络扩容
先做一遍普通的dinic 然后再更改源点为超级源,超级源向原源加一条capacity=k && cost=0的边,再加上有费用的边跑最小费用最大流 #include <iostr ...
- Linux基础命令详解-1
本篇详解的命令有以下30个 1.cd 功能:切换工作目录 参数列表 2.ls 功能:查看目录里的内容 参数列表 3.mv 功能: 移动或重命名文件和目录 参数列表 4.pwd ...
- linux系统管理员 第五部分 1认识系统服务
linux系统管理员 一 认识系统服务 二认识与分析登录文件 三启动流程.模组管理与loader 四网络设定与备份策略 五软件的安装 源代码与tarball 六软件的安装rpm srpm与yum ...
- 【LeetCode】Available Captures for Rook(车的可用捕获量)
这道题是LeetCode里的第999道题. 题目叙述: 在一个 8 x 8 的棋盘上,有一个白色车(rook).也可能有空方块,白色的象(bishop)和黑色的卒(pawn).它们分别以字符 &quo ...
- Centos6.5搭建git远程仓库
远程仓库搭建 step1:安装git ```yum -y install git``` step2:创建用户git,用来运行git服务 useradd git passwd git //修改git用户 ...
- java之线程池面试题
面试官:线程池有哪些?分别的作用是什么? 常用的线程池有: newSingleThreadExecutor newFixedThreadExecutor newCacheThreadExecutor ...
- 【译】NCCloud: Applying Network Coding for the Storage Repair in a Cloud-of-Clouds
NCCloud:多云存储设备下存储修复的网络编码 Yuchong Hu, Henry C. H. Chen, Patrick P. C. Lee, Yang Tang 摘要:近年来的研究提出通过条带 ...
- BS4(BeautifulSoup4)的使用--find_all()篇
可以直接参考 BS4文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find-all 注意的是: 1.有些 ...
- 洛谷 P1522 牛的旅行
题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,Farmer John就有多个 ...
- 前端开发之html篇
一.什么是html? 1.我们说socket网络编程的时候,提到过一个cs模型,就是客户端—服务端模型,前端开发也是基于网络编程,但是这时就应该是bs模型了,是浏览器与服务端的通信. 我们可以模拟一个 ...