2016-08-01 72 views
-1

我试图编写一个程序,要求学生姓名,一些其他数值,并通过它们的数值将它们分配给组,以使所有组都为接近尽可能相等(通过获取列表中最高的下一个值,并将其分配给下一个组,等等)。在python循环中有一个不断变化的变量

但是,我需要将它们的编号保存到某个变量以及它们的名称中,然后打印出该组的列表。 为此,我需要一个变量,每次循环添加另一个学生时都会变化。我还需要对这些数字进行排序,然后以某种方式回复它们在被分组后分享的名称,并且我不确定如何执行其中的任何操作。有没有办法做到这一点,我将不得不使用另一种语言?

这是我到目前为止的代码:

from easygui import * 
times = 0 
name = 0 


s_yn = ynbox("Would you like to enter a student?") 
while s_yn == 1: 
    msg = "Student's Information" 
    title = "House Sorting Program" 
    fieldNames = ["Name", "Grade","Athleticism (1-10)","Intellect (1-10)","Adherance to school rules (1-10)"] 
    fieldValues = [] 
    fieldValues = multenterbox(msg,title, fieldNames) 

    times = times + 1 

    ath = fieldValues[2] 
    int_ = fieldValues[3] 
    adh = fieldValues[4] 
    ath = int(ath) 
    int_ = int(int_) 
    adh = int(adh) 
    total = ath+int_+adh 

    s_yn = ynbox("Would you like to enter a student?") 
+0

您有问题要问? – zeantsoi

+0

你是对的,我的不好 –

+0

我认为部分答案可能是使用[defaultdict](https://docs.python.org/3/library/collections.html#collections.defaultdict),它默认为一个空的数组,然后按照他们的总分添加学生的姓名。例如'my_default_dict [合计] .append(student_name)'。只能想到尝试将它们分组并使它们尽可能地得分的天真方式。 – freebie

回答

0

我相信这将是很好的创建保持了与学生相关联的所有变量一个学生类。然后,您可以将每个学生添加到列表中,您可以根据所需的值进行排序,并将其划分为您想要的组数。

from easygui import * 
from operator import attrgetter 


class Student(object): 

    def __init__(self, name, grade, athleticism, intellect, adherance): 
     self.name = name 
     self.grade = int(grade) 
     self.athleticism = int(athleticism) 
     self.intellect = int(intellect) 
     self.adherance = int(adherance) 
     self.total = self.athleticism + self.intellect + self.adherance 

    def __str__(self): # When converting an instance of this class to a string it'll return the string below. 
     return "Name: %s, Grade: %s, Athleticism (1-10): %s, Intellect (1-10): %s, Adherance to school rules (1-10): %s"\ 
       % (self.name, self.grade, self.athleticism, self.intellect, self.adherance) 


student_group = [] 
while ynbox("Would you like to enter a student?"): # Returns 'True' or 'False' so it'll loop every time the user press 'yes'. 
    message = "Student's Information" 
    title = "House Sorting Program" 
    field_names = ["Name", "Grade", "Athleticism (1-10)", "Intellect (1-10)", "Adherance to school rules (1-10)"] 
    field_values = multenterbox(message, title, field_names) 

    student = Student(*field_values) # Unpack all elements in the list 'field_values' to the initializer. 
    student_group.append(student) # Add the student to the group 'student_group'. 


# When the user has put in all the students we sort our group by 'total' (or any other value you want to sort by). 
sorted_group = sorted(student_group, key=attrgetter("total"), reverse=True) 

# Just as an example I divided the students into 3 groups based on their total. 
best_students = sorted_group[:len(sorted_group) // 3] 
average_students = sorted_group[len(sorted_group) // 3:2 * len(sorted_group) // 3] 
worst_students = sorted_group[2 * len(sorted_group) // 3::]