Program 8— Social Security Number Validation
In this project, you will write a program to prompt the user to enter their social security number in the format ###-##-#### and then determine if the input is properly formatted. A properly formatted social security number will have:
• A length of 11
• Hyphens (“-“) in the 3rd and 6th characters - Remember you start with 0!
• There are 9 characters that are digits (0-9)
Two examples of the program in operation:
Enter your SSN: 555-55-5555
The SSN is VALID!
Enter your SSN: 55555-555
The SSN is INVALID!
Development Process
A template for this project is provided with comments to help guide you as you write the program to accomplish the following steps:
1. Download the template for Program 8 and save it to a folder on your computer/flash drive.
2. View/print the following programs from the Chapter 12 course module in Blackboard as examples to follow as you complete this program:
a. Program 12-5 – shows how to check the length of a string and the number of digits in a string
b. Program 12-6 – shows how to check for symbols in specific positions within a string
3. Open the template program file.
4. Add code to the numberDigits() function so that it will return the number of digits contained in the string stored in the str parameter. See example program 12-5.
5. Add code to the isValidFormat() function so that it will return True if the SSN is properly formatted and False if it is not properly formatted. See example program 12-6.
6. Add code to the main() function that calls the other two functions and uses the results that they return to determine if the SSN is valid or invalid and print the appropriate message. See example program 12-5.
7. Debug your code and try multiple test cases to see if it works properly.

#Template for Program 8
#Complete the code as discussed in the Program 8 Instructions

def main():

    #Get a SSN from the user
    mySSN = input("Enter your SSN (###-##-####): ")

    #Validate the SSN and print valid/invalid



#Write a function to count the number of digits in the string
#Replace the word pass with your code
def numberDigits(str):
    pass

#Write a function to check if there is a hyphen "-" in the
#3rd and 6th positions in the string
#Replace the word pass with your code
def isValidFormat(str):
    pass

#Start by calling main
main()

This is what I have so far and i cant figure out why it isnt working

def main():
    mySSN = input("Enter your SSN (###-##-###):")
    if len(mySSN) >= numberDigits(mySSN)>=11 and isValidFormat(mySSN)>=valid:
        print("The SSN is VALID")
    else:
        ("The SSN is INVALID")

def numberDigits(str):
    count=0
    for n in str:
        if n.isdigit():
            count = count + 1

def isValidFormat(str):
    if len(str) == 11 and str[4 and 7] == "_":
        valid = True 

    else:
        valid = False


main()
def isValidFormat(str):
    if len(str) == 11 and str[4 and 7] == "_":
        valid = True
    else:
        valid = False

You are testing for an underscore and not a hyphen in this part of your code. Try this instead:

def isValidFormat(str):
    if len(str) == 11 and str[4 and 7] == "-":
        valid = True
    else:
        valid = False

You also have an error on lines 2 and 3. Do you see what they are?

Edited 1 Year Ago by rubberman

This article has been dead for over six months. Start a new discussion instead.