how do you get the first letter of a name from user input and put it into a if loop?
Question:
I have the task of a book for Python where I need the user to input their name. I then have to check the first letter of that name and print it out in uppercase letters IF it starts with a vocal. Else, I have to tell the user that their name does not begin with a vocal. Now my program keeps spitting out that it doesn’t start with a vocal even though the input DID start with a vocal. Here’s the latest variation:
name = input()
if name[0] == 'A''a''E''e''I''i''O''o''U''u':
print(f"{name.upper}")
else:
print("Your name doesn't start with a vocal!")
I tried my best to change around what could prevent from getting the first letter, etc. But I don’t know other methods than to get the letter from a substring, so maybe there is another way? Whatever, I’d like to know what the issue is.
Answers:
This is an efficient way to do it
Change the if
:
if name[0] in "AaEeIiOoUu":
print(f"{name.upper()}")
Rest of the code can stay the same.
The mistake in your original code was to place the options one-after-another
That is the same as just concatenating them into one huge string. Your original code was, in effect, testing whether the first character of name
was, literally, AaEeIiOoUu
, which of course it could never be.
To prove the source of the error, try this code
test = "A" 'b' 'C' 'd' """E"""
print(test)
You will get
AbCdE
Equal operator checks equality, you probably want to check if the first letter is a vowel, so the in operator is the right thing to use.
name = input()
if name[0].lower() in ('a', 'e', 'i', 'o', 'u'):
print(f"{name.upper()}")
else:
print("Your name doesn't start with a vocal!")
I have the task of a book for Python where I need the user to input their name. I then have to check the first letter of that name and print it out in uppercase letters IF it starts with a vocal. Else, I have to tell the user that their name does not begin with a vocal. Now my program keeps spitting out that it doesn’t start with a vocal even though the input DID start with a vocal. Here’s the latest variation:
name = input()
if name[0] == 'A''a''E''e''I''i''O''o''U''u':
print(f"{name.upper}")
else:
print("Your name doesn't start with a vocal!")
I tried my best to change around what could prevent from getting the first letter, etc. But I don’t know other methods than to get the letter from a substring, so maybe there is another way? Whatever, I’d like to know what the issue is.
This is an efficient way to do it
Change the if
:
if name[0] in "AaEeIiOoUu":
print(f"{name.upper()}")
Rest of the code can stay the same.
The mistake in your original code was to place the options one-after-another
That is the same as just concatenating them into one huge string. Your original code was, in effect, testing whether the first character of name
was, literally, AaEeIiOoUu
, which of course it could never be.
To prove the source of the error, try this code
test = "A" 'b' 'C' 'd' """E"""
print(test)
You will get
AbCdE
Equal operator checks equality, you probably want to check if the first letter is a vowel, so the in operator is the right thing to use.
name = input()
if name[0].lower() in ('a', 'e', 'i', 'o', 'u'):
print(f"{name.upper()}")
else:
print("Your name doesn't start with a vocal!")