Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to let Python recognize both lower and uppercase input?

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 uppercase letters as input. For example, if I provide the word "Apple" as input, the result is True; however, if the word "apple" is provided as input, the result is False. How do I fix it?

word = input ("Please Enter a word:")
if (word [1] =="A") :
    print("The word begins with a vowel")
elif (word [1] == "E") :
    print("The word begins with a vowel")
elif (word [1] == "I") :
    print("The word begins with a vowel")
elif (word [1] == "O") :
    print("The word begins with a vowel")
elif (word [1] == "U") :
    print("The word begins with a vowel")
else:
    print ("The word do not begin with a vowel")
like image 208
user1718826 Avatar asked Oct 04 '12 02:10

user1718826


1 Answers

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")
like image 182
Blender Avatar answered Sep 19 '22 03:09

Blender