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位 ...
随机推荐
- ACdream 1135 MST
MST Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Problem Descrip ...
- c语言头文件以及make注意事项
c语言头文件以及make注意事项 头文件说明:自己定义的头文件和项目文件放在一起,注意使用""而不是使用<>,系统的头文件才使用<> 当main函数要调用其 ...
- HDU-4612 Warm up,tarjan求桥缩点再求树的直径!注意重边
Warm up 虽然网上题解这么多,感觉写下来并不是跟别人竞争访问量的,而是证明自己从前努力过,以后回头复习参考! 题意:n个点由m条无向边连接,求加一条边后桥的最少数量. 思路:如标题,tarjan ...
- BZOJ 2154 Crash的数字表格 ——莫比乌斯反演
求$\sum_{i=1}^n\sum_{j=1}^n lcm(i,j)$ 枚举因数 $ans=\sum_{d<=n} F(d) * d$ $F(d)$表示给定范围内两两$\sum_{gcd(i, ...
- 西南民大oj 1762 我的式子不可能那么难写 【波兰式】
描述 啦啦啦.作为一个苦逼的程序猿.?.请看下图... 现在老总想让你帮他儿子写个简单计算器(他儿子小学3年级,嘘!),写不出来就扣奖金..快帮他写吧... 给一个包含+-*/()的正确的表达式.要你 ...
- JS控制背景音乐 没有界面
建立一个HTML5页面,放置<audio>标签,设置音频文件源,设置循环播放.准备两张图片,分别表示开启和暂停背景音乐两种状态,可以点击. <audio id="music ...
- Admin组件-----Django自带
1 list_display from django.contrib import admin # Register your models here. from app01.models impor ...
- leetcode 318. Maximum Product of Word Lengths
传送门 318. Maximum Product of Word Lengths My Submissions QuestionEditorial Solution Total Accepted: 1 ...
- Powerdesigner 使用小技巧
1.table与table之间:改直角为直线; 2.Name 和code 不联动
- BZOJ——1610: [Usaco2008 Feb]Line连线游戏
http://www.lydsy.com/JudgeOnline/problem.php?id=1610 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 2 ...