Answer to Question #292439 in Python for iryce

Question #292439

You task is to write a program that will continuously add friend data to a list named friends until the user enters "No". The program must display the contents of friend list.


friend's data to be collected:

Last name

First name

Birthdate

Gender

Contact No.



Validation:

Name must not be empty.

Must check the validity of the birth date.

Gender either F or M only.

Contact No. must be exactly 11 digits.


Display "Invalid Input" and ask the user to input another value.

Input

Friend's data

Tortor
Rhea
12/25/1990
F
09172345678
Yes
Nayang
Iya
11/11/1995
F
09172415679
No

Output

Name: Rhea Tortor
Birthdate: December 25, 1990
Gender: Female
Contact No.: 09172345678

Name: Iya Nayang
Birthdate: November 11, 1995
Gender: Female
Contact No.: 09172415679





1
Expert's answer
2022-01-31T07:56:03-0500
import datetime


def validate_data(data, title):
   error_message = 'Invalid input! Please, input another value'

   if data == '':
       print(error_message)
       return None
   elif (title == 'birthday'):
       try:
           result = datetime.datetime.strptime(data, '%m/%d/%Y')
       except:
           print(error_message)
           return None
       else:
           return result
   elif title == 'gender':
       if (data == 'F'):
           return 'Female'
       elif (data == 'M'):
           return 'Male'
       else:
           print(error_message)
           return None
   elif (title == 'contact'):
       if len(data) == 11:
           try:
               int(data)
           except:
               print(error_message)
               return None
           else:
               return data
       else:
           print(error_message)
           return None
   else:
       return data 

if __name__ == '__main__':
   friends = list()
   titles = [
       'last_name',
       'first_name',
       'birthday',
       'gender',
       'contact'       
   ]

   index = 0
   while True:
           data = input()
           if data == 'No':
               break
           else:
               data = validate_data(data, titles[index])
               if data:
                   friends.append(data)
                   index += 1

   print("Name:", friends[1], friends[0])
   print("Birthday:", friends[2].strftime('%B %d, %Y'))
   print("Gender:", friends[3])
   print("Contact No.:", friends[4])

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS