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基础小练习的更多相关文章

  1. python基础小知识,is和==的区别,编码和解码

    1.is和==的区别 1)id() 通过id()我们可以查看到一个变量表示的值在内存中的地址 >>> s1 = "Tanxu" >>> s2 = ...

  2. python基础--小数据池,代码块的最详细、深入剖析

    本文转至太白金星 一,id,is,== 在Python中,id是什么?id是内存地址,那就有人问了,什么是内存地址呢? 你只要创建一个数据(对象)那么都会在内存中开辟一个空间,将这个数据临时加在到内存 ...

  3. python基础-小练习

    三级菜单 要求: 打印省.市.县三级菜单 可返回上一级 可随时退出程序 购物车程序 要求: 用户名和密码存放于文件中,格式为:egon|egon123 启动程序后,先登录,登录成功则让用户输入工资,然 ...

  4. python基础小点

    变量的命名规则 由字母.下划线.数字组成,且不能以数字开头 不能用关键字作为变量名 最好不要与python内置的一些方法和类名冲突 变量名应尽量简短且具有意义,多个单词之间用下划线连接 注释 #  - ...

  5. Python基础-小程序练习(跳出多层循环,购物车,多级菜单,用户登录)

    一. 从第3层循环直接跳出所有循环 break_flag = False count = 0 while break_flag == False: print("-第一层") wh ...

  6. python基础之 数据类型的补充,小数据类型

    1.id is详解 ID 在Python中,id是什么?id是内存地址,比如你利用id()内置函数去查询一个数据的内存地址: name = 'nba' print(id(name)) # 158583 ...

  7. Python基础知识(六)------小数据池,集合,深浅拷贝

    Python基础知识(六)------小数据池,集合,深浅拷贝 一丶小数据池 什么是小数据池: ​ 小数据池就是python中一种提高效率的方式,固定数据类型使用同一个内存地址 代码块 : ​ 一个文 ...

  8. python基础练习-猜年龄、编写登陆接口小程序

    python基础练习:   一.猜年龄 , 可以让用户最多猜三次! age=40 count = 1 while count <=3 : user_guess=int(input("i ...

  9. Python开发【第二篇】:Python基础知识

    Python基础知识 一.初识基本数据类型 类型: int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647 在64位 ...

随机推荐

  1. java 协程框架quasar gradle配置

    https://github.com/puniverse/quasar-gradle-template/blob/master/gradle/agent.gradle 1.将其中的"-jav ...

  2. 关于EGE图形库在CodeBlocks下的配置

    非常感谢[地球]呵呵@的细心帮助,我差点都放弃了! 我这里使用的是Code::Blocks svn 10595 与 TDM-GCC 5.10 首先下载为 GCC 5.0 以上编译好的 EGE 文件,稍 ...

  3. poj 3617Best Cow Line

    Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year&quo ...

  4. 九度oj 题目1109:连通图

    题目描述: 给定一个无向图和其中的所有边,判断这个图是否所有顶点都是连通的. 输入: 每组数据的第一行是两个整数 n 和 m(0<=n<=1000).n 表示图的顶点数目,m 表示图中边的 ...

  5. BZOJ 1185 [HNOI2007]最小矩形覆盖 ——计算几何

    程序写的太垃圾,卡不过去. GG,甘拜下风. #include <map> #include <cmath> #include <queue> #include & ...

  6. SGU 275 To xor or not to xor【最大xor和 高斯消元】

    题目大意:给你n个数(n<=100)要你找出若干个数使他们的异或和最大 思路:高斯-若当消元消完以后削成若干个独立的行向量,将它们异或起来就好 #include<cstdio> #i ...

  7. [SDOI2008]仪仗队 (欧拉函数)

    题目描述 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线所及的学生人数来判断队伍是否整齐(如下图 ...

  8. 【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers

    http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状 ...

  9. uva 10870 递推关系矩阵快速幂模

    Recurrences Input: standard input Output: standard output Consider recurrent functions of the follow ...

  10. 启动第一个 KVM 虚机

    本节演示如何使用 virt-manager 启动 KVM 虚机. 首先通过命令 virt-manager 启动图形界面 1 # virt-manager 点上面的图标创建虚机 给虚机命名为 kvm1, ...