Answer to Question #290220 in Python for John

Question #290220

Friends

by Rhea Tortor

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
No

Output


Name:·Rhea·Tortor
Birthdate:·December·25,·1990
Gender:·Female
Contact·No.:·09172345678
1
Expert's answer
2022-01-24T12:28:29-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