#setBirthday sets their Birthday to a date
def setBirthday(self):
while True:
try:
day = int(raw_input('Please enter the date of the month (1-31) you were born on here ->'))
if day <= 0 or day >= 32:
print "better try again..."
except ValueError:
continue
else:
break
month = int(raw_input('Please enter the month of the year (1-12) you were born on here ->'))
if month <= 0 or day >= 13:
print "better try again..."
except ValueError:
continue
else:
break
year = int(raw_input('Please enter the year you were born (19xx or 20xx) here ->'))
self.birthday = datetime(year, month, day)
print str(varPerson.getName()) + " you were born on " + str(month) + "/" + str(day) + "/" + str(year)
縮進錯誤就在varPerson的最後3行之上。我嘗試過並嘗試使這種情況與異常一起工作,以便能夠有一個流暢的運行腳本,以便在值不合適時允許進行額外的嘗試。建議?我一直在使用這個鏈接幫助:對應的except塊Try and catch method,python,indentation error can not put it out
Asking the user for input until they give a valid response
這是在Windows上運行.. – Staley
@ Obj3ctiv3_C_88:赦免? 'if __name__ =='__main __':'在所有平臺上都是可選的。 – cdarke
爲什麼你在循環中放置了一堆方法定義?還有一個'嘗試'?你是否期望定義出錯,需要重做? – user2357112