2017-10-14 463 views
0

我必須根據用戶輸入計算一組值的平均等級,並在計算平均值之前刪除最低等級。除了代碼降低最低值外,我擁有一切。我嘗試使用min函數,但是當在循環內部打印時,我得到「TypeError:'int'對象不可迭代。」任何想法如何解決這個問題?謝謝!在Python中檢測用戶輸入中的最低值3.6

numuser=eval(input("How many users are there?: ")) 
numgrade=eval(input("How many grades will there be for each user?: ")) 
usercount=0 
gradecount=0 

while usercount <= numuser: 
    name=input("Please enter the user's first and last name: ") 
    gradetot=0 
    while gradecount < numgrade: 
     gradeval=eval(input("Please enter the grade: ")) 
     gradetot=gradetot+gradeval 
     gradecount=gradecount+1 

low = min(gradeval) 
tot = gradetot-low 

print("The average grade for", name, "is :", tot/gradecount) 
numuser=numuser-1 
usercount=usercount+1 
gradecount=0 
+1

不要使用'eval()'!使用'int()'轉換爲整數。 – Cfreak

+1

至於你的問題。如果你想使用'min()'來獲得最低成績,你需要做一個列表。您正在通過循環將'gradeeval'重置爲整數。或者,您可以使用另一個變量,並且每次都設置它低於前一個變量。 – Cfreak

+0

感謝您的回覆。如果數字取決於用戶的輸入,我會如何將它列入清單? (對不起,如果這是一個愚蠢的問題,有史以來第一次編碼!) – Hannah

回答

0

我修改了您的應用程序,請參閱下面的內容,並嘗試瞭解需要哪些更改才能滿足您的要求。

numuser=eval(input("How many users are there : ")) 
numgrade=eval(input("How many grades will there be for each user : ")) 
usercount=0 
gradecount=0 
gradeval = [] # you need a array to store a range of values 

while usercount <= numuser: 
    name=input("Please enter the user's first and last name : ") 
    gradetot=0 
    while gradecount < numgrade: 
     val=eval(input("Please enter the grade : ")) 
     gradeval.append(val) 
     gradetot=gradetot+val 
     gradecount=gradecount+1 

low = min(gradeval)# min function need a range of values 
tot = gradetot-low 

print("The average grade for : ", name, "is : ", tot/gradecount) 
numuser=numuser-1 
usercount=usercount+1 
gradecount=0