#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Colorado
    Posts
    11
    Rep Power
    0

    Rerunning something when else returns false


    So I'm using Textwrangler and running my script through Terminal. Everything in the script works fine on its own, but as an example, if I enter the birthdate in incorrectly, instead of asking me again for it, it just keeps moving on through the script. How do I repeat that part until all the criteria has been met?

    Here is my script, it's simple and it's my first python program. I'm following along with the CS course for MIT online.

    Code:
    birthdate = raw_input('Enter your birthdate here.')
    if len(birthdate) > 0:
        print 'Good job. Now:'
    else:
        print 'I told you to enter your birthdate.'
    
    last_name = raw_input('Enter your last name here.')
    if len(last_name) > 0 and last_name.isalpha():
    	print "Good job. You entered:" + ' ' + last_name + ' ' + birthdate + '.'
    else:
    	print 'I told you to enter your last name, dammit. Pay attention!'
    As an example, if I go in and enter nothing when it asks for my birthdate, it will print "I told you to enter your birthdate." but it moves on after that and makes you enter your last name.
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,673
    Rep Power
    1958
    You will have to use a while loop
    Something like this should work: (untested)
    Code:
    missingBirthDate = true
    while missingBirthDate:
    	birthdate = raw_input('Enter your birthdate here.')
    	if len(birthdate) > 0:
    		print 'Good job. Now:'
    		missingDate = false
    	else:
    		print 'I told you to enter your birthdate.'
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Colorado
    Posts
    11
    Rep Power
    0
    Sweet, thanks a ton. I'm just getting into programming, so I hadn't gotten schooled up on while loops.
    That did the trick, well, once I figured out that the true and false had to be capitalized.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Since empty strings evaluate to boolean False, this:
    Code:
    if len(birthdate) > 0:
    can simply be replaced by:
    Code:
    if birthdate:
    -Mek
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    510
    Rep Power
    33
    I prefer a function, and you should also check that a valid date was entered.
    Code:
    def get_birthdate():
        while True:
            birthdate = raw_input('Enter your birthdate here.')
            if len(birthdate) > 0:
                print 'Good job. Now:'
                return birthdate
    
            print "You entered an incorrect date-->try again"
    Last edited by dwblas; May 12th, 2013 at 02:01 PM.

IMN logo majestic logo threadwatch logo seochat tools logo