You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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_case function didn't accept a parameter, so it couldn't access the input string s.
  • You tried to assign swap_case = s.swapcase instead of calling the swapcase() 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:39:26