def convert_case(text):
"""Converts a string from lowercase to uppercase and vice versa.
Args:
text: The string to be converted.
Returns:
The converted string.
"""
converted_text = ""
for char in text:
if char.islower():
converted_text += char.upper()
elif char.isupper():
converted_text += char.lower()
else:
converted_text += char # Leave non-alphabetic characters unchanged
return converted_text
Get input from the user
text = input("Enter a string: ")
Convert the case and print the result
converted_text = convert_case(text)
print("Converted string:", converted_text)
```
Explanation:
1. `convert_case(text)` function:
- Takes a string `text` as input.
- Iterates through each character in the string using a `for` loop.
- For each character:
- If the character is lowercase (`char.islower()`), convert it to uppercase (`char.upper()`) and add it to `converted_text`.
- If the character is uppercase (`char.isupper()`), convert it to lowercase (`char.lower()`) and add it to `converted_text`.
- If the character is neither lowercase nor uppercase (e.g., a number, space, or punctuation), leave it unchanged and add it to `converted_text`.
- Returns the `converted_text`.
2. Getting input:
- `text = input("Enter a string: ")` prompts the user to enter a string and stores it in the `text` variable.
3. Converting and printing:
- `converted_text = convert_case(text)` calls the `convert_case` function to convert the string and store the result in `converted_text`.
- `print("Converted string:", converted_text)` displays the converted string to the user.
How to run the code:
1. Save the code as a Python file (e.g., `case_converter.py`).
2. Open a terminal or command prompt.
3. Navigate to the directory where you saved the file.
4. Run the code using the command `python case_converter.py`.
5. The program will ask you to enter a string. Type your string and press Enter.
6. The converted string will be displayed.