1
我創建了一個名爲bootcamp的類,它允許用戶添加完成的任務,查看他們應該執行的所有任務,並檢查未完成的任務。問題是我創建了一個算法來檢查進度的百分比,我把完成列表的長度分配到總任務列表的長度上。我最初認爲它沒有記錄這些變化,因爲它總是返回零,但在用其他操作數(例如+和*)測試之後,它運行良好。爲什麼師總是返回0?共享代碼將有助於瞭解我的問題:Python類/總是返回0
class Bootcamp(object):
tasks = {1: "TDD", 2: "OOP", 3: "Programming Logic", 4: "Version
Control", 5: "Agile Methodology",
6: "Growth Mindset", 7: "Asking Questions", 8:
"Motivation and Commitment", 9: "Speaking"}
def __init__(self, name, tasks=tasks):
self.name = name
self.tasks = tasks
self.completed = []
self.incompleted = tasks.values()
def add_completed_items(self, i):
if i in self.tasks.keys():
self.completed.append(self.tasks[i])
self.incompleted.remove(self.tasks[i])
return "tasks added to complete"
else:
return "not in the tasks"
nick = Bootcamp("nick")
total_work = int(len(nick.tasks))
print nick.add_completed_items(1)
print nick.add_completed_items(2)
print nick.add_completed_items(3)
comleted_work = int(len(nick.completed))
progress = (comleted_work/total_work) * 100
print nick.incompleted`
print progress`
它的工作.....它給了我很多頭痛的,因爲我認爲我的方法是行不通的。現在我可以在課堂上實現它 – Nix
稍微好一點的解決方案:通過將'from __future__ import division'添加到文件頂部,移至Py3樣式分區。然後'/'會產生浮點結果。如果你想劃分樓層,你可以使用''''明確選擇。 – ShadowRanger