我已經創建了一個函數來要求用戶輸入輸入一天中工作的小時數,整個事情返回正確的,除了如果輸入一個不正確的值,它不會顯示其錯誤信息如果輸入的值小於零,則它會再次輸入。Python 3嵌套While循環
def hours_for_days(week_no,day):
i = -1
input_string = "Enter number of hours for week " + str(week_no) + " " + day + ": "
while i <0
work_time = rawinput(input_string)
try:
i = int(work_time)
except ValueError:
print("Enter a number: ")
else:
while 24 < i
work_time = rawinput(input_string)
try:
i = int(work_time)
except ValueError:
print("Enter a number: ")
else:
if i < 0
print("Enter a number between 0 and 24")
if i > 24
print("Enter a number between 0 and 24")
這更像是一個比一個問題的肯定。 –
好,然後澄清,我怎麼得到錯誤消息「輸入一個介於0和24之間的數字」來打印 – Gunnerlc
@Gunnerlc *「整件事情回報正確」*您確定嗎?代碼中有很多語法錯誤。也許你發佈了錯誤版本的代碼? – jDo