該程序正常工作,但每個if語句中必須重複time =「%s:%s」%(小時,分鐘)。我已將它放在所有其他範圍和地點,但我無法弄清楚它爲什麼必須重複,而不是在if語句或其他位置之外宣佈它。不必要的重複聲明
def minutesToHours():
hour = 0
entry = input("How many minutes? I will convert it to hours...")
if 0 <= entry < 60:
minute = entry
time = "%s:%s" % (hour, minute)
print time
elif entry >= 60:
hour = entry/60
minute = entry % 60
time = "%s:%s" % (hour, minute)
print time
else:
print "Please enter a number greater than zero next time."
minutesToHours()
input()
你不能例如代碼的頂部分配「時間」,因爲「分鐘」尚未定義。 – Totem
Python沒有聲明;這只是一個簡單的任務。 – chepner
在我以前的代碼迭代中,我將頂端的「分鐘」和「小時」分別設置爲零。我只是沒有具體提到我到目前爲止所嘗試過的... – pleaseDontPanic