Let's take a simple example , We all know the grades in the University , It's not the final word , But there are a lot of achievements and different proportions , Finally, there will be a total score , So paying attention to details is the most important ! Let's take a look at this !
Project requirements : There is one CSV The document contains the scores of many students ( A lot of usual results , And the name , Student number ), What this program does is , Use specified algorithms and rules , Calculate the total score , And then rank , Print out the achievement information of each stage as the teaching and research reform . At the same time, the output must be good-looking , write in TXT File file !
Related to knowledge :Python All grammar !
Project features : Formatted print , Make the output more beautiful and intuitive , Function structure , Help code read , Project portability , Simple and easy to understand
Parsing files
def csv_students():
global student_infos
import csv
student_infos = []
with open(r" Detailed results .csv", encoding='utf-8-sig') as file:# Put your CSV The file and the program file are placed under the same folder
f_csv = csv.reader(file)
header = next(f_csv)
for row in f_csv:
student_info = {}
for index in range(6):
student_info[header[index]] = row[index]
student_infos.append(student_info)
# Format print function , Make its output align automatically
add_print1()
Parse its file , Then show it in the form of a dictionary , But the code is optimized here , Make the print look good . Here we import CSV This library , For some of its properties, the operation Syntax , We can have a look at Baidu . First
We set up a list to store all of the dictionaries later , Then I opened the file , And use .reader() Read header information , Then take out the header , This will facilitate our subsequent operations .next()
Then we create the dictionary with a loop , The keys and values in each dictionary are added by ourselves .6 Each title ,6 Messages , We will complete the key and value matching , This creates a complete dictionary , Finally, until we have read these things completely , The cycle is over .
Realization effect :
Calculate the score
def scores_students():
for scores in student_infos:
total = float(scores[" experiment 1"]) * 1 / 10 + float(scores[" experiment 2"]) * 1 / 10 + \
float(scores[" experiment 3"]) * 2 / 10 + float(scores[" Final grade "]) * 6 / 10
scores[" Overall score "] = total
# Rank according to the overall evaluation results , And print it out ( Additional features )
student_infos.sort(key=lambda item: item[" Overall score "], reverse=True)
i = 0
for score in student_infos:
i += 1
score[" Class ranking "] = i
add_print2()
import json
# Will be resolved after CSV file , Convert to python object , And stored in a file ( Additional features )
with open(r"student.txt", "w", encoding="utf_8") as file:
json.dump(student_infos, file, ensure_ascii=False, indent=0)
Segmented printing
def grad_students():
num1 = 0
num2 = 0
num3 = 0
num4 = 0
num5 = 0
for scores in student_infos:
grade = float(scores[" Overall score "])
if grade < 60:
num1 += 1
elif grade >= 60 and grade < 70:
num2 += 1
elif grade >= 70 and grade < 80:
num3 += 1
elif grade >= 80 and grade < 90:
num4 += 1
elif grade >= 90 and grade <= 100:
num5 += 1
all = int(num1 + num2 + num3 + num4 + num5)
print("\t The total number of people is %s people " % (all))
# Print out the number and proportion of fractional segments
print("\t[0, 60) There are {} people ".format(num1), "\t Accounted for as {}".format(str(num1 / all * 100)[:4] + "%"))
print("\t[60,70) There are {} people ".format(num2), "\t Accounted for as {}".format(str(num2 / all * 100)[:4] + "%"))
print("\t[70,80) There are {} people ".format(num3), "\t Accounted for as {}".format(str(num3 / all * 100)[:4] + "%"))
print("\t[80,90) There are {} people ".format(num4), "\t Accounted for as {}".format(str(num4 / all * 100)[:4] + "%"))
print("\t[90,100] There are {} people ".format(num5), "\t Accounted for as {}".format(str(num5 / all * 100)[:4] + "%"))
Find students
def find_students():
while True:
study_num = input(" Please enter the student number you want to find : ")
for score in student_infos:
if score[" Student number "] == study_num:
print(" full name :", score[" full name "], " Student number :", score[" Student number "], " experiment 1:",
score[" experiment 1"], " experiment 2:", score[" experiment 2"], " experiment 3:",
score[" experiment 3"], " Final grade :", score[" Final grade "], " Overall score :",
score[" Overall score "], " Class ranking :", score[" Class ranking "])
continue
It's over , Of course, here we call it a back-end development project , Generally, we call it graphical interface operation development in our mobile phone or educational administration system , This requires our later knowledge .
Click here to download the source code for operation !!!