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.
>Solution :
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