【问题标题】:Try and catch method, python, indentation error can't figure it outTry and catch 方法,python,缩进错误想不通
【发布时间】:2016-02-25 20:40:21
【问题描述】:
 #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 行之上。我已经尝试并尝试让这个场景与异常一起工作,以便能够拥有一个平稳运行的脚本,如果值不合适,可以进行额外的尝试。建议?我一直在使用此链接寻求帮助:

Asking the user for input until they give a valid response

【问题讨论】:

  • 这是在windows中运行的..
  • @Obj3ctiv3_C_88:原谅? if __name__ == '__main__': 在所有平台上都是可选的。
  • 你为什么要把一堆方法定义放在一个循环里?还有try?您是否预计定义会出错并需要重做?
  • @Obj3ctiv3_C_88:不,不是。如果我错了,请告诉我记录在哪里。

标签: python try-catch


【解决方案1】:
  • 您的外部 try 块没有对应的 except 块。
  • 在类定义中直接使用一段时间或尝试块是没有意义的。它们必须在方法定义中。
  • 您不能在类的 while 块内定义方法。

与其尝试修复上述问题,不如尝试仔细查看您的代码并弄清楚您想要做什么。然后,询问如何在 StackOverflow 或其他 StackExchange 网站上最好地实施这个以宏观目标为中心的问题,展示您迄今为止所做的尝试。

【讨论】:

  • 好吧,我一定是真的累了,我完全错过了。谢谢。我会尝试删除它...它总是很明显...
  • @Staley 到底是什么问题?您能否确认问题中的代码 sn-p 是正确的?即使有这个问题,未来的观众也有可能会发现这个问题很有用。如果它解决了您的问题,您也可以使用绿色复选标记接受我的回答。
【解决方案2】:

如果您真的想处理任何异常,请尝试以下代码:

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()

就个人而言,虽然 True 不好用……

【讨论】:

  • ok,如果你删除第一个 while True:,尝试最后一个 except 语句并调整缩进这行得通。
【解决方案3】:

为什么要在循环中定义 setBirthday?

while True:
    try:

        def setBirthday(self):
            while True:
                try:

【讨论】:

    猜你喜欢
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    相关资源
    最近更新 更多