麻烦老师给每行代码注释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # coding:utf-8 def capitalize(data): index = 0 temp = '' for item in data: if index = = 0 : temp = item.upper() # 特别是这里 else : temp + = item index + = 1 return temp result = capitalize( 'hello 小慕' ) print (result) |
5
收起
正在回答 回答被采纳积分+1
1回答
好帮手慕小猿
2022-09-23 13:11:57
同学,你好!同学可以参考以下代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # coding:utf-8 def capitalize(data): index = 0 #定义变量index初始值为0 temp = '' #定义变量temp初始值为空字符串 for item in data: #循环data的每个字母 if index = = 0 : #如果index值等于0 temp = item.upper() # 把当前循环item的值变成大写字母赋值给temp变量 else : #若index值不等于0 temp + = item #将当前的item的值添加到temp变量中 index + = 1 #对index值进行加1 return temp #将temp变量作为函数的返回值 result = capitalize( "hello 小慕" ) #调用capitalize()函数,将"hello 小慕"作为实参传递给data形参,并将函数的返回值赋值给result变量 print (result) #输出result 变量 |
祝学习愉快~
相似问题
登录后可查看更多问答,登录/注册
Python全能工程师
- 参与学习 人
- 提交作业 16329 份
- 解答问题 4470 个
全新版本覆盖5大热门就业方向:Web全栈、爬虫、数据分析、软件测试、人工智能,零基础进击Python全能型工程师,从大厂挑人到我挑大厂,诱人薪资在前方!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧