Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how do you get the first letter of a name from user input and put it into a if loop?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading