首页 文章资讯内容详情

Python程序接受包含所有元音的字符串

2026-06-05 1 花语

有时您希望根据某些条件接受输入。在这里,我们将看到相同类型的程序。我们将编写一个仅允许带有元音的单词的程序。我们将向他们展示输入是否有效。

让我们一步一步地看看方法。

定义元音列表[A,E,I,O,U,a,e,i,o,u]

初始化单词或句子。

遍历单词或句子。

检查列表中是否存在。

3.1.1. If not, break the loop and print Not accepted.

接受其他打印

示例

让我们将文本转换为Python代码。

def check_vowels(string): # vowels vowels = [A, E, I, O, U, a, e, i, o, u] # iterating over the string for char in string: if char not in vowels: print(f"{string}: Not accepted") break else: print(f"{string}: Accepted") if __name__ == __main__: # initializing strings string_1 = "tutorialspoint" string_2 = "AEiouaieeu" # checking the strings check_vowels(string_1) check_vowels(string_2)

输出结果

如果运行上面的代码,您将得到以下结果。

tutorialspoint: Not accepted AEiouaieeu: Accepted

结论

您可以根据需要检查其他属性。如果您对本教程有任何疑问,请在评论部分中提及。