Python字符串大小写互换练习:请求提供测试输入案例
Fixing Your String Case-Swap Code & Test Inputs
Hey there! Let's get your Python code working properly first—right now it has a couple of syntax bugs that will prevent it from running as expected. Then I'll share a range of test inputs to help you thoroughly test the case-swap functionality.
Corrected Code
Here's the fixed version of your code, with explanations of the changes:
def swap_case(s): # Added the parameter 's' so the function receives the input string # Changed to call the swapcase() method (needs parentheses to execute) return s.swapcase() if __name__ == '__main__': s = input() result = swap_case(s) print(result)
The issues in your original code:
- The
swap_casefunction didn't accept a parameter, so it couldn't access the input strings. - You tried to assign
swap_case = s.swapcaseinstead of calling theswapcase()method (missing parentheses) and returning the result.
Test Inputs & Expected Results
Use these test cases to cover different scenarios—they'll help you make sure your code handles all edge cases:
- Mixed standard case:
Input:Hello World!
Expected Output:hELLO wORLD! - All uppercase characters:
Input:I AM LEARNING PYTHON
Expected Output:i am learning python - All lowercase characters:
Input:python is fun
Expected Output:PYTHON IS FUN - With numbers and special symbols (these characters won't be affected by case swapping):
Input:PyThOn_123!@#
Expected Output:pYtHoN_123!@# - Empty input (just press Enter when prompted):
Input:Expected Output: - String with mixed cases and extra spaces:
Input:tHIS Is A tEST String
Expected Output:THIS is a Test sTRING
内容的提问来源于stack exchange,提问作者ranjith sha
相关产品推荐
相关产品推荐

