# 1. Count vowels in a string
text = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = sum(1 for ch in text if ch in vowels)
print(f"Number of vowels: {count}")
# 2. Find the maximum of three numbers
a = int(input("Enter first integer: "))
b = int(input("Enter second integer: "))
c = int(input("Enter third integer: "))
max_num = a
if b > max_num:
max_num = b
if c > max_num:
max_num = c
print(f"Maximum: {max_num}")
# 3. Reverse a string
s = input("Enter a string to reverse: ")
reversed_s = s[::-1]
print(f"Reversed: {reversed_s}")
This code has three separate tasks: counting vowels in a string, finding the maximum of three numbers, and reversing a string. Each part works correctly on its own.
- In the vowel counting part, you use a generator expression with `sum` to count characters that are in the vowel string. This is a concise and correct approach.
- For the maximum of three numbers, you use a series of `if` statements to update a variable. This works, but consider what happens if you need to compare more than three numbers.
- The string reversal uses slicing with `[::-1]`, which is a standard and efficient way to reverse a string in Python.