2014-02-10 29 views
1

我想寫一個計算並打印Python程序如下:製作一個統計程序

  • 的平均分從評分列表
  • 從分數的名單得分最高
  • 得分最高的學生的名字。

程序首先要求用戶輸入個案數量。對於每種情況,程序都應該要求用戶輸入學生人數。對於每個學生,程序要求用戶輸入學生的姓名和標記。對於每個案例,該計劃都會報告得分最高的學生的平均分數,最高分數和名稱。

還 如果在CASE中有多個人得分最高,程序應該只報告第一次出現。 平均得分和最高得分應該有2個小數位。 輸出應該與樣本程序輸出一樣。

我一直在努力,到目前爲止是這樣的:

grade=[] 
name_list=[] 
cases=int(input('Enter number of cases: ')) 
for case in range(1,cases+1): 
    print('case',case) 
    number=int(input('Enter number of students: ')) 
    for number in range (1,number+1): 

     name=str(input('Enter name of student: ')) 
     name_list.append(name) 
     mark=float(input('Enter mark of student:')) 
     grade.append(mark) 


     highest= max (grade) 
     average=(sum(grade)/number) 
     high_name=grade.index(max(grade)) 
     print('average',average) 
     print('Highest',highest) 
     print (high_name) 

這是我迄今破譯。我現在最大的問題是獲得高分的個人的名字。任何想法和反饋非常感謝。至於下面的答案,恐怕唯一不能理解的是字典功能,但其他方面對我來說是有意義的。

+0

你怎麼想象倍增的情況下次數的現有案例列表是要幹什麼? – geoffspear

+0

我希望乘以案件號碼列表會給我案件的輸入。 例如: 假設的情況下,輸入= 2 這樣的話會有2例 我後來想是兩個案件中,我可以添加信息,如條款有待進一步探討建立2所列出:名稱x的學生數,x的學生標記數,學生平均分數,高分數,與高分相關的印刷名稱。然後移動到外殼2 我已經重新啓動我的代碼,使得現在它像: – KingShahmoo

+0

殼體= INT(輸入(「輸入的情況下的量」)) 箱子= [] 對於殼體在範圍(0,情況+1): 個案。append(case * cases) print(cases) student = int(input('Enter the number of students:')) students = [] – KingShahmoo

回答

1

這類似於一個作業,它也是具體的細節。

反正,official docs是開始學習Python的好地方。 它們相當清晰,並且有大量有用的信息,例如

range(start, end):如果開始省略參數,則默認爲0

的部分約lists應該給你一個良好的開端。

+0

感謝您的反饋!!我會轉到上面的區域嘗試解決這個問題。 – KingShahmoo

1
numcases = int(input("How many cases are there? ")) 

cases = list() 

for _ in range(numcases): 
    # the _ is used to signify we don't care about the number we're on 
    # and range(3) == [0,1,2] so we'll get the same number of items we put in 

    case = dict() # instantiate a dict 

    for _ in range(int(input("How many students in this case? "))): 
     # same as we did before, but skipping one step 
     name = input("Student name: ") 
     score = input("Student score: ") 
     case[name] = score # tie the score to the name 

    # at this point in execution, all data for this case should be 
    # saved as keys in the dictionary `case`, so... 
    cases.append(case) # we tack that into our list of cases! 

# once we get here, we've done that for EVERY case, so now `cases` is 
# a list of every case we have. 

for case in cases: 
    max_score = 0 
    max_score_student = None # we WILL need this later 
    total_score = 0 # we don't actually need this, but it's easier to explain 
    num_entries = 0 # we don't actually need this, but it's easier to explain 
    for student in case: 
     score = case[student] 
     if score > max_score: 
      max_score = score 
      max_score_student = student 

     total_score += score 
     num_entries += 1 
     # again, we don't need these, but it helps to demonstrate!! 
    # when we leave this for loop, we'll know the max score and its student 
    # we'll also have the total saved in `total_score` and the length in `num_entries` 
    # so now we need to do..... 
    average = total_score/max_entries 

    # then to print we use string formatting 

    print("The highest score was {max_score} recorded by {max_score_student}".format(
     max_score=max_score, max_score_student=max_score_student)) 
    print("The average score is: {average}".format(average=average)) 
+0

+1好的解釋性意見。我想你應該除以'num_entries'吧?也許可以解釋一下字典是什麼,或者鏈接到文檔。有人開始學習python可能還沒有看到這些。 – M4rtini

+0

令人驚歎的代碼。我仍然是一個初學者,所以我有點迷失在字典的意義上是 我已經更新了我的代碼,但我不知道如何張貼在正確格式的計算器上 – KingShahmoo

+0

grade = [] name_list = [] cases = int(input('Enter number of cases:')) (1,case + 1): print('case',case)(輸入('輸入學生姓名:')) number_in int(輸入(名稱) 標記=浮子(輸入( '輸入學生的標記:')) grade.append(標記) 最高= MAX(級) 平均=(SUM(級)/數) high_name =等級(最高(等級)) print('average',average) print('Highest',highest) print(high_name) 我有...... – KingShahmoo