尝试catch方法,python,缩进错误无法弄清楚[重复]

问题描述 投票:0回答:3
 #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 行上方。我已经尝试过让这个场景与异常一起工作,以便能够有一个平稳运行的脚本,如果值不合适,允许进行额外的尝试。建议?我一直在使用此链接寻求帮助:

询问用户输入,直到他们给出有效的响应

python try-catch
3个回答
5
投票
  • 您的外部 try 块没有相应的 except 块。
  • 直接在类定义中使用 while 或 try 块是没有意义的。它们必须位于方法定义内。
  • 您不能在类的 while 块内定义方法。

不要尝试按原样修复上述问题,而是尝试仔细查看您的代码并弄清楚您想要做什么。然后,在 StackOverflow 或其他 StackExchange 网站上询问如何最好地实现以目标为中心的大局问题,并展示您迄今为止所尝试过的内容。


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

就我个人而言,虽然 True 不太好用...


1
投票

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

while True:
    try:

        def setBirthday(self):
            while True:
                try:
© www.soinside.com 2019 - 2024. All rights reserved.