Pyhon implements the class selection program ^ v^

1.code

# -*- coding:utf-8 -*-
from random import *
import os
import sys

se = "s"
while(type(se) != int):
    try:
        se = int(input("Please set random number seed:"))
        seed(se)
    except:
        print("Input error, please re-enter")
        continue


dict = {}#Set the value corresponding to the name as the key and other data as the key according to your own needs
list = []#name list
list1 = []#The same list as list n = len(list)
lt = []
dict1 = {}
num = 1
result = open("Score statistics.xls",'w',encoding='gbk')
result.write('Student number\t full name\t score\n')
while(n >= 0):
    if n > 0:
            print('')
            x = 'd'
            while(x not in ['T','F','t','f','s','S']):
                try:
                    x = input("Please select Yes to select from all( T)""Or" remaining{}Select from people( F)",Press S Key to exit the program:".format(n))
                    if x == "s" or x == 'S':
                        sys.exit()
                    if x not in ['T', 'F', 't', 'f', 's', 'S']:
                        print("Input error, please re-enter")
                except:
                    continue
            if x == "F" or x == "f":
                print("The first{}Round selection, start".format(num))
                m = "l"
                while (type(m) != int):
                    try:
                        m = int(input("Number of people drawn:"))
                    except:
                        print("Input error, please re-enter")
                        continue
                while(x == 'F' or x == 'f'):
                    if m <= len(list):
                        name = sample(list,m)
                        num += 1
                        print(" ".join(str(i) for i in name))
                        for i in name:
                            list.remove(i)
                            score = 's'
                            while(type(score) != int):
                                try:
                                    score = int(input("Please enter{}Score of:".format(i)))
                                    if i not in dict1.keys():
                                        dict1[i] = int(score)
                                    else:
                                        dict1[i] += int(score)
                                    lt.append([dict[i], i, dict1[i]])
                                    ls = [dict[i], i, dict1[i]]
                                    for j in range(len(ls)):
                                        result.write(str(ls[j]))
                                        result.write('\t')
                                    result.write('\n')
                                except:
                                    '''try:
                                        score = int(input("Please enter{}Score of:".format(i)))
                                        dict1[i] = int(score)
                                        lt.append([dict[i], i, dict1[i]])
                                        ls = [dict[i], i, dict1[i]]
                                        for j in range(len(ls)):
                                            result.write(str(ls[j]))
                                            result.write('\t')
                                        result.write('\n')
                                    except:
                                        continue'''
                                    print("Input error, please re-enter")
                                    pass
                        n = len(list)
                    else:
                        os.system('pause')
                        sys.exit()

                    '''else:
                        name = sample(list, 4)
                        print(" ".join(str(i) for i in name))
                        for i in name:
                            list.remove(i)
                            score = input("Please enter{}Score of:".format(i))
                            dict1[i] = int(score)
                            lt.append([dict[i], i, dict1[i]])
                            ls = [dict[i], i, dict1[i]]
                            for j in range(len(ls)):
                                result.write(str(ls[j]))
                                result.write('\t')
                            result.write('\n')
                        n = len(list)'''
                    break
            elif x == 'T' or x == 't':
                m = "s"
                while (type(m) != int):
                    try:
                        m = int(input("Number of people drawn:"))
                    except:
                        print("Input error, please re-enter")
                        continue
                name = sample(list1,m)
                print(" ".join(str(i) for i in name))
                for i in name:
                    score = 's'
                    while (type(score) != int):
                        try:
                            score = int(input("Please enter{}Score of:".format(i)))
                            if i not in dict1.keys():
                                dict1[i] = int(score)
                            else:
                                dict1[i] += int(score)
                            lt.append([dict[i], i, dict1[i]])
                            ls = [dict[i], i, dict1[i]]
                            for j in range(len(ls)):
                                result.write(str(ls[j]))
                                result.write('\t')
                            result.write('\n')
                        except:
                            '''try:
                                score = int(input("Please enter{}Score of:".format(i)))
                                dict1[i] = int(score)
                                lt.append([dict[i], i, dict1[i]])
                                ls = [dict[i], i, dict1[i]]
                                for j in range(len(ls)):
                                    result.write(str(ls[j]))
                                    result.write('\t')
                                result.write('\n')
                            except:
                                continue'''
                            print("Input error, please re-enter")
                            continue

            else:
                sys.exit()
    else:
        os.system('pause')
        sys.exit()
# -*- coding:utf-8 -*-
'''for i in dict1.keys():
    lt.append([dict[i],i,dict1[i]])
result = open("Score statistics.xls",'w',encoding='gbk')
result.write('Student number\t full name\t score\n')
for i in range(len(lt)):
    for j in range(len(lt[i])):
        result.write(str(lt[i][j]))
        result.write('\t')
    result.write('\n')
result.close()'''

The part commented out in the middle is the code I tried to write when I was writing this program. It has been modified and written many times, but there are still some problems that have not been solved. It will be issued after subsequent solutions ^ v^

2. Problems encountered in preparation

2.1 error reporting of Chinese list

Because most programming software only supports writing in English, although Python supports Chinese in the code, a large number of Chinese are still not fully compatible, so it needs to be used at this time

# -*- coding:utf-8 -*-

This code can run normally

2.2 setting the implementation of extracting people from all and non repeating

  Set multiple judgment statements and give the statements prompted for input to realize the implementation of all people pumping and non repeated people pumping.

2.3 the fault tolerance of the program is low (exit if input error)

Use the try except statement to test whether the input is correct

If it is correct, execute the statement after try; if it is wrong, execute the statement after except

Use the while loop to re-enter the wrong data until it is entered correctly

2.4 the problem that the program will exit directly after all the programs are run

Introduce Python's own os library and use

os.system('pause')

Stop the program and stay on the current page

2.5 program data loss after accidental exit

Random number seed is used to ensure that the data after accidental exit is reproducible to prevent data loss

The disadvantage is that the steps are complicated

3 remaining unresolved issues

3.1 the problem of easy loss of file data

Every time you start the program, the program will reset the excel file, resulting in the loss of existing data

3.2 problems after interrupting the program

After one execution, running the program again needs to start the previous action again, which is more troublesome and depends on the set random number seed

3.3 duplication of data saved in Excel

When selecting all candidates and not repeating candidates, it is inevitable to select the same person. Although a dictionary is set to save the results, it is not finally output to excel. Therefore, if a person has two or more results in the data, the highest score shall prevail

Tags: Mini Program

Posted on Mon, 08 Nov 2021 09:17:50 -0500 by Jeller