1 回答

TA貢獻1877條經(jīng)驗 獲得超6個贊
你的代碼有很多問題。其中之一是
for i in range(1, a):
如果您想要最小值或最大值,這部分沒有意義。您需要遍歷成績列表。
mark并且student考慮到它們可以分別替換為sum和,也是不必要的len。
整個代碼似乎缺乏適當?shù)慕Y(jié)構(gòu)。這是一個示例實現(xiàn)。如果不允許使用sumor len,您可以將自己的mark和student方法帶回來,但盡量不要弄亂并保持可讀性:
def maxx(grades):
if (not grades): # if empty, we let the caller know
return None
res = grades[0] # we know the list is not empty
for i in grades:
if i > res:
res = i
return res
def minn(grades):
if (not grades):
return None
res = grades[0]
for i in grades:
if i < res:
res = i
return res
def main():
grades = [] # list of grades
while (True):
grade = int(input("Enter Mark: "))
if (grade < 0): break
grades.append(grade)
student_cnt = len(grades)
total = sum(grades)
print("Exit")
print("Total students :", student_cnt)
print("The total marks is:", total)
print ("The average marks is:", total / student_cnt)
print("The max marks is :", maxx(grades))
print("The min marks is :", minn(grades))
if __name__ == "__main__":
main()
輸入輸出:
Enter Mark: 30
Enter Mark: 20
Enter Mark: 10
Enter Mark: 40
Enter Mark: -1
Exit
Total students : 4
The total marks is: 100
The average marks is: 25.0
The max marks is : 40
The min marks is : 10
添加回答
舉報