Using startswith() to detect blank input
while True:
familar_name = input('Enter your name: ')
if familar_name.startswith(""):
print('Please enter a valid name')
elif familar_name.isalpha():
print('Hello', familar_name)
break
I'm trying to detect a blank input. elif
statement never met even when entering an alphabetical input.
A string always starts with an empty string, this condition is True. That's why your elif statement doesn't work:
a = "hi"
if a.startswith(""):
print('yes')
output:
yes
In order to check the blank input, you can just check for truthiness of the familar_name
like :
name = input('enter your name')
if name:
...
The above if statement's body will execute if the string is not blank. But this will fail if user passes whitespaces. So maybe you need to use .strip()
before that checking.
Also don't forget to call .isalpha
method --> .isalpha()
use " "
instead of ""
while True:
familar_name = input('enter your name')
if familar_name.startswith(" "): # you have to have a space here.
print('Please enter a valid name')
elif familar_name.isalpha():
print('Hello', familar_name)
break