Update Lesson 3 Task 2 to simplify letter checks and use match-case statement

This commit is contained in:
Stanislav Mykhailenko 2023-01-15 12:48:31 +02:00
parent 062b3646a4
commit 46f0c669d1
GPG key ID: 1E95E66A9C9D6A36

View file

@ -6,13 +6,13 @@
# 0 - OK
def get_vowels(string):
vowels = "AaEeIiOoUu"
result = [each for each in string if each in vowels]
vowels = "aeiou"
result = [each for each in string if each.lower() in vowels]
return result
def get_consonants(string):
consonants = "BbCcDdFfGgHhJjKkLlMmNnPpQqRrSsTtVvWwXxYyZz"
result = [each for each in string if each in consonants]
consonants = "bcdfghjklmnpqrstvwxyz"
result = [each for each in string if each.lower() in consonants]
return result
@ -20,17 +20,18 @@ string = input("Enter a string: ")
operation = input("Enter operation number\n1. Sort the string\n2. Count the elements number\n3. Output vowels\n4. Output consonants\n5. Output the words in reverse order\n6. Output the original string\n\nAny other input will exit the program.\n")
if operation == str(1):
print("Sorted string: " + str(sorted(string)))
elif operation == str(2):
print("String length: " + str(len(string)))
elif operation == str(3):
print("Vowels: " + str(get_vowels(string)))
elif operation == str(4):
print("Consonants: " + str(get_consonants(string)))
elif operation == str(5):
reversed = reversed(string.split (" "))
print("String words in reverse order: ")
print([each for each in reversed])
elif operation == str(6):
print("Original string: " + string)
match operation:
case '1':
print("Sorted string: " + str(sorted(string)))
case '2':
print("String length: " + str(len(string)))
case '3':
print("Vowels: " + str(get_vowels(string)))
case '4':
print("Consonants: " + str(get_consonants(string)))
case '5':
reversed = reversed(string.split (" "))
print("String words in reverse order: ")
print([each for each in reversed])
case '6':
print("Original string: " + string)