Writing a program that prompts the user to enter a social security number in the format ddd-dd-dddd
where d
is a digit. The program displays "Valid SSN"
for a correct Social Security number or "Invalid SSN"
if it's not correct. I nearly have it, just have one issue.
I'm not sure how to check if it's in the right format. I can enter for instance:
99-999-9999
and it'll say that it's valid. How do I work around this so that I only get "Valid SSN"
if it's in the format ddd-dd-dddd
?
Here's my code:
def checkSSN():
ssn = ""
while not ssn:
ssn = str(input("Enter a Social Security Number in the format ddd-dd-dddd: "))
ssn = ssn.replace("-", "")
if len(ssn) != 9: # checks the number of digits
print("Invalid SSN")
else:
print("Valid SSN")