Python Function to Check if a Character is a Vowel
Here's a Python function to determine if a character is a vowel:
def check_vowel(char):
vowels = ['a', 'e', 'i', 'o', 'u']
if char.lower() in vowels:
print(f"The character '{char}' is a vowel.")
else:
print(f"The character '{char}' is not a vowel.")
# Example usage:
user_input = input("Enter a character: ")
check_vowel(user_input)
Explanation:
check_vowel(char): This function takes a single character (char) as input.vowels = ['a', 'e', 'i', 'o', 'u']: A list containing all lowercase vowels is created.char.lower() in vowels: The function converts the input character to lowercase and checks if it's present in thevowelslist.print(f"The character '{char}' is a vowel."): If the character is found, it prints a message indicating it's a vowel.print(f"The character '{char}' is not a vowel."): If the character is not found, it prints a message indicating it's not a vowel.- User Input: The code prompts the user to enter a character, stores it in the
user_inputvariable, and calls thecheck_vowelfunction with this input.
原文地址: https://www.cveoy.top/t/topic/paBR 著作权归作者所有。请勿转载和采集!