【问题标题】:Check if letter in the alphabet is a vowel, if so print the vowel, otherwise print the consonant (python) [duplicate]检查字母表中的字母是否是元音,如果是则打印元音,否则打印辅音(python)[重复]
【发布时间】:2021-04-30 16:19:03
【问题描述】:

我正在开始使用python,这看起来应该很简单,但无法破解。

我必须创建一个包含字母表的列表,然后打印每个项目是元音还是辅音。我可以使用一些帮助。现在它只是告诉我每个字母都是辅音。 谢谢。


alphabet =['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

for letter in alphabet:
    if letter in alphabet == ['a','e','i','o','u']:
        print (f'{letter} is a vowel')
    else:
        print(f'{letter} is a consonant')

【问题讨论】:

  • 是的,你不需要在字母表中做 == ['a','e','i','o','u'],只要 ['a' 中的字母,'e','i','o','u']
  • 试着用普通的英文单词来解释你希望letter如何与元音列表相关联。注意你是如何不提到“字母”这个词的?注意在描述列表之前你是如何使用“in”这个词的吗?请注意您几乎直接在“in”之前使用了“letter”这个词? Python 语法类似。

标签: python loops


【解决方案1】:

我不知道if letter in alphabet == ['a','e','i','o','u'] 是什么意思。

相反,尝试这样的事情。

alphabet =['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

for letter in alphabet:
    if letter in ['a','e','i','o','u']:
        print (f'{letter} is a vowel')
    else:
        print(f'{letter} is a consonant')

【讨论】:

    猜你喜欢
    • 2023-02-03
    • 2022-11-18
    • 1970-01-01
    • 2020-06-20
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    相关资源
    最近更新 更多