import sys msg="qingQIAO_HighSchool" def my_function(s): for i in s: if 97 <= ord(i) <= 122: # 对小写字母数值判断 upper_num = ord(i) - 32 # 转换得到大写字母数值 print(chr(upper_num),end="") # 通过数值返回大写字母并打印 elif 65 <= ord(i) <= 90 : # 对大写字母数值判断,转换成小写字母 lower_num = ord(i) + 32 print(chr(lower_num), end="") else: print(i,end="") # 其他字符不变直接打印 print("转换后的字符串为:",end="") my_function(msg.swapcase())这串代码对吗
时间: 2024-01-10 14:02:49 浏览: 30
这段代码有一些问题,缺少了函数调用的部分。可以将最后一行代码修改为 `my_function(msg.swapcase())` 来调用函数。此外,函数中的 `print("转换后的字符串为:",end="")` 应该移动到函数调用的外面,否则会在函数调用时重复打印该句话。修改后的代码如下:
```python
import sys
msg = "qingQIAO_HighSchool"
def my_function(s):
for i in s:
if 97 <= ord(i) <= 122: # 对小写字母数值判断
upper_num = ord(i) - 32 # 转换得到大写字母数值
print(chr(upper_num), end="") # 通过数值返回大写字母并打印
elif 65 <= ord(i) <= 90: # 对大写字母数值判断,转换成小写字母
lower_num = ord(i) + 32
print(chr(lower_num), end="")
else:
print(i, end="") # 其他字符不变直接打印
print("\n转换后的字符串为:", end="")
my_function(msg.swapcase())
```
这段代码的作用是将字符串中的大写字母转换为小写字母,将小写字母转换为大写字母,其他字符不变,并输出转换后的字符串。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)