2016-08-01 107 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::]