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位 ...
随机推荐
- Python 对Mysql的操作
Mysql链接不同的数据库 如果python的模板是按照mysql来写的,后面数据库更换为了Oracle,难道需要重现再来写,当然不是,python提供了API接口,只要编写是面对api,后面的链接会 ...
- 爬虫开发python工具包介绍 (3)
本文来自网易云社区 作者:王涛 :arg str url: URL to fetch :arg str method: HTTP method, e.g. " ...
- IS-IS IGP
is-is 是igp的一种 属于osi的协议 OSI的三层是网络层 包含两种服务 一种是面向连接服务CONS 另一种是无连接服务CLNS CLNS中包含CLNP ...
- 图论trainning-part-1 F. Highways
F. Highways Time Limit: 1000ms Memory Limit: 10000KB 64-bit integer IO format: %lld Java class ...
- Leetcode 365.水壶问题
水壶问题 有两个容量分别为 x升和 y升的水壶以及无限多的水.请判断能否通过使用这两个水壶,从而可以得到恰好 z升的水? 如果可以,最后请用以上水壶中的一或两个来盛放取得的 z升 水. 你允许: 装满 ...
- Linux中执行shell脚本命令的4种方法总结
bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限. 方法一:切换到shell脚本所在 ...
- javascript异常cannot read property xx of null 的错误
一般报这种异常或者错误,是因为试图从null中再读一个属性导致的. 比如:var myAttr=myObj.data.Name; 假如这个时候myObj.data是null,那么再试图读取data的N ...
- 九度oj 题目1335:闯迷宫
题目描述: sun所在学校每年都要举行电脑节,今年电脑节有一个新的趣味比赛项目叫做闯迷宫.sun的室友在帮电脑节设计迷宫,所以室友就请sun帮忙计算下走出迷宫的最少步数. 知道了最少步数就可以辅助控制 ...
- Java:Session详解
以下情况,Session结束生命周期,Servlet容器将Session所占资源释放:1.客户端关闭浏览器2.Session过期3.服务器端调用了HttpSession的invalidate()方法. ...
- Annual Congress of MUD
Annual Congress of MUD 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Multiuser dungeon games, also called MUD games ...