2013-01-17 107 views
0
def open_marks(): 
    N = (int(input('how many students? '))* 5) 
    students = [] 
    for line in open('marks.txt').readlines(): 
     datafile = (line.strip().split('\t')[0].split(',')) 
     for n in datafile: 
      students.append(int(n)) 
    students=students[:N] 
    return students 
def open_marks1(): 
    students = open_marks() 
    students1=students[0::5]#set to return only the first(lowest) marks drawn 
    return students1 
def open_marks2(): 
    students = open_marks() 
    students2=students[1::5]#set to return only the second marks drawn 
    return students2 
def open_marks3(): 
    students = open_marks() 
    students3=students[2::5]#set to return only the third marks drawn 
    return students3 
def open_marks4(): 
    students = open_marks() 
    students4=students[3::5]#set to return only the fourth marks drawn 
    return students4 
def open_marks5(): 
    students = open_marks() 
    students5=students[4::5]#set to return only the fifth(highest) marks drawn 
    return students5 


def count_ranges_one(): 

    students1 = open_marks1() 
    print('first number: ',students1) 
    range_counts1 = [0] * 12 
    for num in students1[:]:#change number to select number of draws 
     which_range=int(num//5) 
     range_counts1[which_range] = range_counts1[which_range] + 1 
    return range_counts1 
def count_ranges_two(): 
    students2 = open_marks2() 
    print('second number: ',students2) 
    range_counts2 = [0] * 12 
    for num in students2[:]:#change number to select number of draws 
     which_range=int(num//5) 
     range_counts2[which_range] = range_counts2[which_range] + 1 
    return range_counts2 
def count_ranges_three(): 
    students3 = open_marks3() 
    print('third number: ',students3) 
    range_counts3 = [0] * 12 
    for num in students3[:]:#change number to select number of draws 
     which_range=int(num//5) 
     range_counts3[which_range] = range_counts3[which_range] + 1 
    return range_counts3 
def count_ranges_four(): 
    students4 = open_marks4() 
    print('fourth number: ',students4) 
    range_counts4 = [0] * 12 
    for num in students4[:]:#change number to select number of draws 
     which_range=int(num//5) 
     range_counts4[which_range] = range_counts4[which_range] + 1 
    return range_counts4 
def count_ranges_five(): 
    students5 = open_marks5() 
    print('fifth number: ',students5) 
    range_counts5 = [0] * 12 
    for num in students5[:]:#change number to select number of draws 
     which_range=int(num//5) 
     range_counts5[which_range] = range_counts5[which_range] + 1 
    return range_counts5 

我在格式標記學生的文本文件: 7,5,10,25,32 9,15,25,39,18 等 以上組根據位置標記(1,2,3,4或5),然後有一個例程'寫'直方圖'以顯示標記的分佈。我寫的是非常笨拙和重複性的,但是我無法弄清楚循環函數將數據分組爲每個問題的標記,然後提供直方圖的每個範圍的數量。請有人可以幫我解決這個問題。Python中循環,從而減少重複

回答

2

你知道你可以將參數傳遞給你的函數嗎?五個open_marks[1-5]功能可以概括爲這樣:

def open_marks_for_student(n): 
    students = open_marks() 
    return students[n-1::5] 

如果現在通過如3的功能,你會得到相同的結果,從舊open_marks3功能:

open_marks_for_student(3) 

同樣的原則也適用於count_ranges功能,只寫一個廣義函數,並傳遞標誌作爲參數:

def count_ranges(marks): 
    range_counts = [0] * 12 
    for num in marks[:]: #change number to select number of draws 
     which_range=int(num//5) 
     range_counts[which_range] = range_counts1[which_range] + 1 
    return range_counts 

現在這樣使用它:

def get_range_counts(): 
    range_counts = [] 
    student_nums = [1,2,3,4,5] #or range(1,6) 
    for n in student_nums: 
     marks = open_marks_for_student(n) 
     range_counts.append(count_ranges(marks)) #create histogram, append to list 
    return range_counts 

仍然有一些優化一個可以作出如只打開一次標記文件而不是每個學生一次,或者在count_ranges的循環中刪除marks上的切片(如果您打算修改循環中的原始列表,則只需要遍歷副本),但這應該足夠了讓你開始。

+0

非常感謝。將通過您的建議,並嘗試執行 – user1478335

+0

我按照你的建議。發生的問題是,當打開IDLE時,系統會詢問您輸入的學生人數,然後在輸入get_range_counts()後,程序會再次詢問五次此信息,然後提供正確的答案。這顯然是因爲函數調用了open_marks五次。這是我無法改變的 – user1478335