2016-02-25 57 views
-5
#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

+0

這是在Windows上運行.. – Staley

+1

@ Obj3ctiv3_C_88:赦免? 'if __name__ =='__main __':'在所有平臺上都是可選的。 – cdarke

+0

爲什麼你在循環中放置了一堆方法定義?還有一個'嘗試'?你是否期望定義出錯,需要重做? – user2357112

回答

1

如果你真的要處理任何異常,請嘗試以下代碼:

import datetime 
from datetime import datetime 


class Person(object): 
    def __init__(self, name): 
     self.name = name 
     self.birthday = None 


#getName returns the name of the person   
    def getName(self): 
     return self.name   

#setBirthday sets their Birthday to a date 
    while True: 
     try: 

      def setBirthday(self): 
       while True: 
        try: 
         day = int(raw_input('Please enter the date of the month (1-31) you were born on here ->')) 
         month = int(raw_input('Please enter the month of the year (1-12) you were born on here ->')) 
         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) 
        except ValueError: 
         print "better try again...return to the start of the loop" 
         continue 
        else: 
         break 
       #if day <= 0 or day >= 32: 
        #print "Please enter a number between 1 and 31. Try again." 

#getAge returns how many days old the individual is  
      def getAge(self): 
       dateNow = datetime.now() 
       dateBirth = self.birthday 
       timedelta = dateNow - dateBirth 
       print str(varPerson.getName()) + " you have been living on Earth for " + str(timedelta) 
#current date - birthdate to get specific days, timedelta 
     except: 
      pass 


varPerson = Person(raw_input('Please enter your name here ->')) 
varPerson.setBirthday() 
varPerson.getAge() 

就個人而言,雖然真正的不好用...

+0

使用此代碼片段它是一個無限循環.. – Staley

+0

好吧,如果您刪除第一個True :,嘗試和最後一個except語句並調整這個工作的縮進。 – Staley

5
  • 你的外try塊沒有。
  • 它有一段時間沒有意義,或者直接在類定義中嘗試塊。它們必須在方法定義內。
  • 您無法在類中的while塊中定義方法。

而不是試圖解決上述問題,請嘗試仔細研究您的代碼並確定您想要執行的操作。然後,詢問如何在StackOverflow或另一個StackExchange站點上最好地實現這個大問題的以目標爲焦點的問題,並展示迄今爲止您嘗試過的內容。

+0

好吧,我一定很累,我完全錯過了。謝謝。我會嘗試刪除它...總是顯而易見的東西... – Staley

+0

@Staley究竟是什麼問題?你能否確認你的問題代碼段是正確的?即使這個問題有一個機會,未來的觀衆可能會發現這個問題很有用。如果解決了您的問題,您也可以使用綠色複選標記接受我的答案。 –

1

你爲什麼要在循環中定義setBirthday?

while True: 
    try: 

     def setBirthday(self): 
      while True: 
       try: 
+0

我忘了刪除該部分,並在發佈之前重置代碼.. – Staley