marks=[3, 5, 4, 2, 5, 5, 3, 5, 4, 4, 4]
d=len(marks)
s=0
for s in range(d):
f=42/d
print("The final grade:" str(f))
uj5u.com熱心網友回復:
關于您的代碼的一些說明:
marks = [3, 5, 4, 2, 5, 5, 3, 5, 4, 4, 4]
# this is OK, but you can just use len(marks), unless you want to save the value
d = len(marks)
# you initialise s, but that's not needed if you plan to use it as a loop variable
s=0
# you make s loop over the length of marks, but you can just loop over marks
for s in range(d):
# this number appears out of nowhere, and you're dividing 42 by the length?
f=42/d
print("The final grade:" str(f))
你追求的是:
marks = [3, 5, 4, 2, 5, 5, 3, 5, 4, 4, 4]
number_of_marks = len(marks)
total = 0
for mark in marks:
total = mark
result = total / number_of_marks
print("The final grade:" str(result))
但是有一個更簡單的方法來做同樣的事情:
marks = [3, 5, 4, 2, 5, 5, 3, 5, 4, 4, 4]
result = sum(marks) / len(marks)
print("The final grade:" str(result))
uj5u.com熱心網友回復:
嘗試這個:
marks=[3, 5, 4, 2, 5, 5, 3, 5, 4, 4, 4]
ms = 0
for s in marks:
ms =s
f = ms/len(marks)
print("The final grade:" str(f))
uj5u.com熱心網友回復:
試試這個:marks=[3, 5, 4, 2, 5, 5, 3, 5, 4, 4, 4]
毫秒 = 0
對于標記中的 s:ms =s
f = ms/len(標記)
print("最終成績:" str(f))
print
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/362954.html
上一篇:如何將輸入的值與用戶輸入相加
下一篇:C vector列印出奇怪的元素
