首页 文章

检查单词的第一个字母是否为元音

提问于
浏览
5

在我提出这个问题之前,我想让你知道我不是一个经验丰富的程序员,而是有人在codecademy.com上学习python的基础知识 .

我正在尝试使用python编写一个函数来检查给定单词的第一个字母,例如“ball”是否为大写或小写的元音 . 例如:

#here is a variable containing a word:
my_word = "Acrobat"

#letters in vowel as a list
the_vowel = ["a","e","i","o","u"]

如何检查“Acrobat”中的第一个字母是否是列表中的元音之一?我还需要考虑它是大写还是小写?

11 回答

  • 14

    试试 my_word[0].lower() in the_vowel

  • 9

    这是我如何做到的,因为在将输入的单词存储为变量之前需要首先检查输入的单词:

    original = raw_input('Enter a word:')
    
    if len(original) > 0 and original.isalpha():
        word = original.lower()
        first = word[0]
        if first in ['a','e','i','o','u']:
            print "vowel"
        else:
            print "consonant"
    else:
        print 'empty'
    
  • 0

    我不知道它是否比这里已经发布的答案更好,但你也可以这样做:

    vowels = ('a','e','i','o','u','A','E','I','O','U')
    myWord.startswith(vowels)
    
  • 10

    以下是一些可以帮助您解决问题的提示 .

    从字符串下标中获取单个字母的字符串 .

    >>> 'abcd'[2]
    'c'
    

    请注意,第一个字符是字符零,第二个字符是字符1,依此类推 .

    接下来要注意的是大写字母不等于小写字母:

    >>> 'a' == 'A'
    False
    

    幸运的是,python字符串有方法 upperlower 来改变字符串的大小写:

    >>> 'abc'.upper()
    'ABC'
    >>> 'a' == 'A'.lower()
    True
    

    要测试列表中的成员资格,我们 in

    >>> 3 in [1, 2, 3]
    True
    >>> 8 in [1, 2, 3]
    False
    

    因此,为了解决您的问题,将下标绑定到一个字母, upper / lower 来调整大小写,并使用 in 测试成员资格 .

  • 1

    anti vowel Function

    def anti_vowel(text):
        vowel = ["a","e","i","o","u"]
        new_text = ''
        for char in text:
            if char.lower() in vowel:
                continue
            else:
                new_text += char
        print new_text
        return new_text
    
  • 1

    我的代码看起来像这样 .

    original = raw_input("Enter a word:")
    word = original.lower()
    first = word[0]
    vowel = "aeiou"
    
    if len(original) > 0 and original.isalpha():
        if first in vowel:
            print word
            print first
            print "vowel!"
        else:
            print word
            print first
            print "consonant
    
  • 0
    my_word = "Acrobat"
    the_vowel = "aeiou"
    
    if myword[0].lower() in the_vowel:
        print('1st letter is a vowel')
    else:
        print('Not vowel')
    
  • 2

    感谢alexvassel为我提供了缺失的部分;)

    so here is the solution to the exercice on codecadmy.com

    original = raw_input('Enter a word:')
    
    word = original.lower()
    
    first = word[0]
    
    vowel = "aeiou"
    
    
    
    if len(original) > 0 and original.isalpha():
    
       if first in vowel:
    
           print 'vowel'
    
           else:   
    
           print 'consonant'
    
    else:
    
      print 'empty'
    
  • 2

    是不是(稍微)将the_vowel定义为字典而不是列表?

    the_vowel = {"a":1,"e":1,"i":1,"o":1,"u":1}
    my_word[0].lower() in the_vowel
    
  • 0
    x = raw_input("Enter a word: ")  
    vowels=['a','e','i','o','u']
    for vowel in vowels:
        if vowel in x:
            print "Vowels"
        else:
            print "No vowels"
    

    这将打印出5行,如果其中任何一行包含一个表示元音的行,那么这意味着有一个元音 . 我知道这不是最好的方式,但它有效 .

  • 4
    x = (input ("Enter any word: "))
    vowel = "aeiouAEIOU"
    if x[0] in vowel:
        print ("1st letter is vowel: ",x)       
    else:
        print ("1st letter is consonant: ",x)
    

相关问题