2015-06-12 54 views
0

一個有三個較大的標誌是我的代碼不能正常工作當我試圖進入字典我不可能做標記和退出循環,沒有它發起

students = int(raw_input()) 
student_inf = {} 
student_marks = [] 
for i in range(0,students): 
    student_name = raw_input() 
    student_inf[student_name] = {} 
    for entry in student_marks: 
    ---->>>> student_inf[student_name][entry] = int(raw_input(entry)) 
print str(sum(student_inf[student_name].values())/3.0) 
+0

什麼是你想在這裏做你正在嘗試'循環一個空列表「什麼是你想要的輸出 – The6thSense

+2

進入student_marks: - 列表student_marks是空的,此循環從不執行。 –

+0

student_marks在執行時爲空 – Maxzeroedge

回答

1

您正在嘗試的地方迭代一個空列表。 做的正確的方式,這將是:

students = int(raw_input()) 
student_inf = {} 
student_marks = [] 
for i in range(0,students): 
    student_name = raw_input() 
    student_inf[student_name] = {} 
    student_marks = [int(x) for x in raw_input().split()] 
    for entry in student_marks: 
     student_inf[student_name][entry] = entry 
    print str(sum(student_inf[student_name].values())/3.0) 

隨着輸入爲:

2 #Number of students tom #Student Name 23 34 #Student marks separated by spaces dick #Student Name 34 43 #Student marks separated by spaces

輸出: 19.0 25.6666666667

+0

反正有人在同一行上輸入名字和標記@Vaulstein –

+0

'raw_input()'可以用來傳遞'n'個用戶參數。然後可以使用split()()(< - 基於空格拆分)將值分別存儲在不同的變量中。 在你的情況下,執行'student = raw_input()',然後如果你確定第一個參數是名字,那麼'student_name = student [0]'rest store'student_marks = student [1:]' – Vaulstein

相關問題