I am using a function for my validation for string. I am currently encountering an error with whitespace how should I do modify my code for users able to input
def Update_StringValidation(message):
while True:
inputs = input(message).lower()
if inputs.isalpha():
return inputs
else:
print("Invalid option. please only enter alphabets")
this is my input code for user i have one more function for Y/N for that my function of validation is working
update_user = vaildate_YN(f"Are u sure to update (Y/N)? ")
if update_user == "y":
# selected customer name to update
print(
f"Package Name: {found_package['Package Name']} Customer Name: {found_package['Customer Name']} "
f"Number of Pax: {found_package['Number of Pax']} Package Cost (per pax): {found_package['Package Cost (per pax)']}")
customer_name = Update_StringValidation("Enter updated customer name: ")
package_name = Update_StringValidation("Enter updated package name: ")
number_pax = update_IntValidation("Enter updated pax:")
pax_cost = update_IntValidation("Enter updated cost per pax: $")
found_package["Customer Name"] = customer_name
found_package["Package Name"] = package_name
found_package["Number of Pax"] = number_pax
found_package["Package Cost (per pax)"] = pax_cost
print("Successfully updated")
# Display successfully updated
print(
f"Package Name: {found_package['Package Name']} Customer Name: {found_package['Customer Name']} "
f"Number of Pax: {found_package['Number of Pax']} Package Cost (per pax): {found_package['Package Cost (per pax)']}")
CodePudding user response:
print(' '.isalpha())
Result : False
Your condition isalpha
is too restrictive for your needs
CodePudding user response:
You can use a regular expression to perform string checks, like this one: ^[a-zA-Z] $
.
You can use it in your program like this:
import re
pattern = re.compile(r"^[a-zA-Z] $")
def Update_StringValidation(message):
while True:
inputs = input(message).lower()
if pattern.match(inputs)
return inputs
else:
print("Invalid option. please only enter alphabets")
Proof of work: link