Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8525617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:02:07+00:00 2026-06-11T08:02:07+00:00

I am working my way through some Python tutorials one of the things that

  • 0

I am working my way through some Python tutorials one of the things that keeps coming up is user input and i just wanted to check that i am validating it correctly and not going about it the long way around.

I have written the code below and only need to ask for the day month and year but if i needed to start asking for address phone number name etc etc this would grow and grow is that normal?

def get_input( i ):

    while True:
        # We are checking the day
        if i == 'd':
            try:
                day = int( raw_input( "Please Enter the day: " ) )
                # If the day is not in range reprint
                if day > 0 and day < 32:
                    #Need to account for short months at some point
                    return day
                else:
                    print 'it has to be between 1 and 31'
            except ( ValueError ):
                print "It has to be a number!"
        elif i == 'm':
            # We are checking the month
            month = raw_input( 'Please enter ' +
                              'in words the month: '
                              ).strip().lower()
            if month in months: # use the dict we created
                return month
            else:
                print 'Please check you spelling!'
        elif i == 'y':
            # Now the year
            try:
                year = int( raw_input( "Please Enter the year" +
                                       "pad with 0's if needed: " ) )
                #make we have enough digits and a positive
                if year > 0 and len( year ) == 4:
                    return year
            except ( ValueError, TypeError ):
                    print "It has to be a four digit number!"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:02:09+00:00Added an answer on June 11, 2026 at 8:02 am

    Why don’t you just have the user input the whole date in one go, and try it out to validate it?

    from time import strptime
    
    def get_date():
        while True:
            date = raw_input("Please enter a date in DD/MM/YYYY format: ")
            try:
                parsed = strptime(date, "%d/%m/%Y")
            except ValueError as e:
                print "Could not parse date: {0}".format(e)
            else:
                return parsed[:3]
    
    year, month, day = get_date()
    

    This will catch errors like 29/2/2011 but accept valid inputs like 29/2/2012.

    If you’d like to accept several formats, just make a list of the format strings you want to accept and try them out one after another on the input, until you find one that works. But watch out for the problem of usage overloading.

    For validating phone numbers I’d just go for a regexp. If you’ve never used regexps before there’s a nice python regexp howto here. Addresses are very free form, so I don’t think I’d bother validating them beyond restricting the length and doing basic security checking, especially if you’re accepting international addresses.

    But in general, if there’s a python module for it you should just try to create an instance based on the input and catch the errors, like I do for the time module in the above example.

    Don’t even try to validate names. Why not? Look at this article. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working my way through some ASP.NET MVC reading and I have a web
I'm still working my way through some pretty basic Actionscript programming (in Flex), and
While working my way through the Android tutorials, I came across something I don't
I am working my way through some samples, and came up with this working
I'm just working my way through Django, and really liking it so far, but
Im working my way through some C++ code and came across the following void
I'm working my way through some unit testing with AndroidTestCase and was wondering, if
I am working on a Python script that digs through a directory full of
I'm working my way through "Learning jQuery" (Third Edition). In Chapter 4: "Manipulating the
I'm working my way through displaying various data elements from parent & child data,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.