How to let Python recognize both lower and uppercase input?

前端 未结 6 1853
野性不改
野性不改 2020-12-11 21:28

I am new to Python. I am writing a program that distinguishes whether or not a word starts with a vowel. The problem is, that the program is only able to correctly handle up

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-11 21:55

    Convert the word entirely to lowercase (or uppercase) first:

    word = input("Please Enter a word:").lower()  # Or `.upper()`
    

    Also, to get the first letter of your word, use word[0], not word[1]. Lists are zero-indexed in Python and almost all programming languages.

    You can also condense your code by quite a bit:

    word = input("Please Enter a word:")
    
    if word[0].lower() in 'aeiou':
        print("The word begins with a vowel")
    else:
        print("The word do not begin with a vowel")
    

提交回复
热议问题