0% found this document useful (0 votes)
31 views3 pages

Data Validation Solution

The document contains multiple Python code snippets for user input validation. It includes code to validate a number between 10 and 20, count vowels in a sentence, validate phone numbers in a specific format, and check password strength based on various criteria. Each section prompts the user for input and ensures the data meets specified requirements before proceeding.

Uploaded by

tomcao2809
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views3 pages

Data Validation Solution

The document contains multiple Python code snippets for user input validation. It includes code to validate a number between 10 and 20, count vowels in a sentence, validate phone numbers in a specific format, and check password strength based on various criteria. Each section prompts the user for input and ensures the data meets specified requirements before proceeding.

Uploaded by

tomcao2809
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

##Write a python code to validate an input (from keyboard) number.

#It should be between 10 and 20, inclusive. Otherwise, prompt the user repeatedly.

numeric = 0
# Create a flag that we will use to get us out of the validation loop.
notGood = True
while notGood:
data = input("Enter a number between 10 and 20 inclusive: ")
while not data.isdigit():
data = input("Enter a number between 10 and 20 inclusive: ")

# If you make it to this point you know that you have a clean string
# of digits, no letters or other dirt in the data.

# Convert to an int.
numeric = int(data)

# Now you can check to see if it falls in your valid range.


if numeric > 9 and numeric < 21:
notGood = False

# If you make it to this point you know you have an integer in


# the range 10-20 inclusive.
print("The number is ", numeric)

###################################################################################
########
#count vowels in a sentence and print them all

#vowelsInString
vowels= ["a","e","i","o","u"]
#vowels="aeiou"
sentence=input('Enter a sentence:')
vowelsList=[]
numberVowels=0
for letter in sentence:
if letter in vowels:
numberVowels=numberVowels+1
#print (letter)#will print every vowel
vowelsList.append(letter)
print(vowelsList)
print(numberVowels)

###################################################################################
########

#validatePhoneNumbers
# This format (###)###-####, index0, index 4 and index 8

phoneNumberValid=False
specialchar=['(',')','-']
while not phoneNumberValid:
Number=input("Enter your phone code, or -1 to exit")
if Number=="-1":
break

if len(Number)==13:
if Number[0]== specialchar[0] and Number[4]== specialchar[1] and\
Number[8]== specialchar[2] and Number[1].isdecimal() and\
Number[2].isdecimal() and Number[3].isdecimal() and
Number[5].isdecimal() and\
Number[6].isdecimal() and Number[7].isdecimal()and
Number[9].isdecimal()and\
Number[10].isdecimal()and Number[11].isdecimal()and
Number[12].isdecimal():
print ("Valid phoneNumber",Number)
phoneNumberValid=True
else:
print("Invalid phone number, Try again!")

###################################################################################
########

# Function to validate phone number


# Data must be this pattern and nothing else (n = number)
# (nnn)nnn-nnn
# Try yourself!

def isValid(raw):
# Input: raw typing from Human
# Return: True or False, is this string a valid phone number or not.

#========== start of main line of program ===============


phone = ""
# Instead of placing the entire data entry loop inside a function we
# often use this approach. The data entry loop is in the main line but
# we move the data validation chunk of code to a function. Both
# approaches are good.

while True:
phone = input("phone number (nnn)nnn-nnnn: ")
if isValid(phone):
break

# Done. print the valid phone number.


print(phone)

###################################################################################
########

#Write a module to validate password


#The password should have:
# at least one uppercase letter
# at least one lowercase letter
# at least one digit
# has only alphanumeric chatacters (i.e. no spaces, only letters or digits)
# at least a length of 10 characters

#---------module starts here--------


def validate_password():
#initialise variables
u= 0
l = 0
d =0
ch = 0
flag = 1
while flag==1:
password= input ("please enter your password: ")
if (len(password) <10):
print("Invalid password! requires 10 characters. Try again!")

if (len(password) >= 10):


for i in password:

# counting uppercase alphabets


if (i.isupper()):
u+=1

# counting lowercase alphabets


if i.islower():
l +=1

# counting digits
if i.isdigit():
d +=1

if (u>= 1 and l>= 1 and d>= 1 and u+l+d == len(password)):


print ("Password is valid: ", password)
flag =0
else:
print("Invalid password! requires at least 1 uppercase, 1 lowercase, 1
digit, , no space allowed and all characters must be alphanumeric. Try again!")

#---------main starts here--------

password= validate_password()

You might also like