Question: Write a Python code that asks users for their last name, validates it (i.e., checks it against business rules), and then displays it as confirmation

Write a Python code that asks users for their last name, validates it (i.e., checks it against business rules), and then displays it as confirmation with only the first letter capitalized, e.g., Smith. If the name is not valid per our business rules, the display should be Invalid Name instead.

The rules for last names at our business are:

Names must be no longer than 20 characters but must be at least two characters.

Names may have an apostrophe (e.g., ORourke) or a hyphen (e.g., Smith-Barnes) but no other punctuation, no spaces, and no numbers.

For example, we dont allow Smith Jr. (has space and period) or Smith III (has space).

Here is the code I have so far but it doesn't seem to work,

last_name = input("Enter your last name: ") length=len(last_name) rule_one=False if(length<=20 and length>=2): rule_one=True i=0 rule_two=True while i=65 and ord(last_name[i])<=90) is_upper_case=(ord(last_name[i])>=97 and ord(last_name[i])<=122) if(not(is_lower_case or is_upper_case or ord(last_name[i])==45 or ord(last_name[i])==39)): rule_two=False i=i+1 if(rule_one and rule_two): print(last_name[:1].upper() + last_name[1:].lower()) else: print("Invalid Name")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!