How to let Python recognize both lower and upperca

2019-02-25 06:08发布

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")

6条回答
Emotional °昔
2楼-- · 2019-02-25 06:12

You could convert the input to upper case before comparing.

查看更多
乱世女痞
3楼-- · 2019-02-25 06:21

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")
查看更多
欢心
4楼-- · 2019-02-25 06:25

The check for vowels is done using str.startswith which can accept a tuple of multiple values. PEP 8 Style Guide for Python Code recommends the use of startswith with over string slicing for better readability of code:

Use ''.startswith() and ''.endswith() instead of string slicing to check for prefixes or suffixes.

Conditional Expressions are used to set the message indicating whether the word starts with a vowel or not. Then I used the String Formatting method to prepare the message. Also just as a English grammar correction thing I replaced the sentence "The word do not begin with a vowel" with "The word does not begin with a vowel".

word = input("Please Enter a word:")
is_vowel = 'does' if word.lower().startswith(tuple('aeiou')) else 'does not'
print("The word {} begin with a vowel".format(is_vowel))
查看更多
小情绪 Triste *
5楼-- · 2019-02-25 06:26

1) There are many ways to do what needs to be done, like what Blender said. However, what you are trying to do is to convert the first letter to upper whether the input is upper or lower. Use 'capitalize' to do this.

2) You also need to use word[0], instead of word[1] to get the first letter out

word = raw_input("Please Enter a word: ").capitalize()

if word [0] in "AEIOU" :
    print("The word begins with a vowel")
else:
    print ("The word does not begin with a vowel")

This will make the first letter in upper case and the rest will remain as is.

查看更多
霸刀☆藐视天下
6楼-- · 2019-02-25 06:36

You should use:

word[i] in 'AEIOUaeiou'
查看更多
等我变得足够好
7楼-- · 2019-02-25 06:38

Usually you would use str.lower() (or str.upper()) on the input to normalise it.

Python3.3 has a new method called str.casefold() which works properly for unicode

查看更多
登录 后发表回答