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

The indentation error is just above varPerson the last 3 lines. I have tried and tried to get this scenario working with exceptions to be able to have a smooth running script that allows for additional tries if value is not appropriate. Suggestions? I have been using this link for help:

Asking the user for input until they give a valid response

Community
  • 1
  • 1
Staley
  • 55
  • 2
  • 11

3 Answers3

5
  • Your outer try block does not have a corresponding except block.
  • It doesn't make sense have a while or try block directly in a class definition. They must be inside a method definition.
  • You cannot define methods inside a while block in a class.

Instead of trying to fix the above as they stand, try taking a good look at your code and figuring out what it is that you want to do. Then, ask how to best implement that big-picture objective-focused question on StackOverflow or another StackExchange site, showing what you've tried so far.

Yatharth Agarwal
  • 4,385
  • 2
  • 24
  • 53
  • ok well I must be really tired, I totally missed that. Thanks. I'll try removing that...its always something obvious... – Staley Feb 25 '16 at 20:49
  • @Staley What exactly was the problem? Can you confirm that your code snippet in the question was correct? There is a chance even with this question that future viewers might find the question useful. You may also accept my answer using the green checkmark if it solved your problem. – Yatharth Agarwal Feb 25 '16 at 20:50
1

Why are you defining setBirthday in a loop?

while True:
    try:

        def setBirthday(self):
            while True:
                try:
Cosinux
  • 321
  • 1
  • 4
  • 16
1

If you really want handle any exceptions, try below code:

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

Personally, While True is not good to use...

be_good_do_good
  • 4,311
  • 3
  • 28
  • 42
  • ok if you remove the first while True:, try and the last except statement and adjust the indentation this works. – Staley Feb 25 '16 at 21:10