It’s either remove middleName variable from the print statement and insert it between the firstName and lastName variables in the print statement on the condition (conditional statement) when a user enters a middle name, OR, leave middleName variable in the print statement and strip the white space between firstName and middleName variables when a user does not enter a middle name, because as the code is now, a huge space is left between the first and last name in the output when a user does not enter a middle name. How do I code these conditional processes; would like the option to use either? I’ve searched elsewhere for the code to no avail.
Here's my code:
import re
print('Political Elections Voter Eligibility\n\n')
pattern1 = "^[A-Z]|^[A-Z]+'[A-Z]|^[A-Z]+-[A-Z]"
pattern2 = "^[A-Z]|^[A-Z].|^[A-Z]+'[A-Z]|^[A-Z]+-[A-Z]"
pattern3 = "^[A-Z]|^[A-Z]+'[A-Z]|^[A-Z]+-[A-Z]|^[A-Z]+ [A-Z]|^[A-Z]+ [A-Z]."
while True:
firstName = input("Enter your first name and tap or click 'Enter': ")
print('\n')
if re.search(pattern1, firstName):
break
else:
print('\n')
print('Invalid entry.')
while True:
middleName = input("Enter your middle name or middle initial and tap or click 'Enter' (Optional): ")
print('\n')
if len(middleName)==0 or re.search(pattern2, middleName):
break
else:
print('\n')
print('Invalid entry.')
while True:
lastName = input("Enter your last name and tap or click 'Enter': ")
print('\n')
if re.search(pattern3, lastName):
break
else:
print('\n')
print('Invalid entry.')
while True:
try:
age = int(input("Enter your age and tap or click 'Enter': "))
except ValueError:
print('\n')
print('Invalid entry.')
continue
print('\n')
if 1 <= age <= 125:
if 1 <= age <= 17:
print('%s %s %s, age %s, you are not eligible to vote in political elections.' % (firstName, middleName, lastName, age))
break
if 18 <= age <= 125:
print('%s %s %s, age %s, you are eligible to vote in political elections.' % (firstName, middleName, lastName, age))
break
else:
print('Invalid entry.')