PTA的Python练习题(七)
不知不觉一个星期过去了,继续从 第3章-12 求整数的位数及各位数字之和 开始做题
1.
我原来想用题目漏洞做题的,不过想想还是算了自己写个完整的:
a=int(input()) b=len(str(a))-1 d=b+1 count=0 for i in range(0,d): c=a/(10**b) c=int(c) e=c-1 count=count+c a=a-10**e*c b=b-1 print(‘{} {}‘.format(d,count))
这道题的int类型是不能直接求位数的,会报错,需要强制类型转换成str算length(代码第二行)
2.
s = input() ss = ‘‘ for c in s: if "A" <= c <= "Z": ss += chr(155 - ord(c)) else: ss += c print(ss)
知识点:ord()函数。返回对应的ascii码值,Z(90)+A(65)=155
chr()函数,返回对应的字符
3.
上面的代码拿下来稍微修改一下:
s = input() ss = ‘‘ for c in s: if c==‘#‘: break if "A" <= c <= "Z": ss += chr(ord(c) + 32) else: ss += c print(ss)
很简单大小写的ASCII码差32
相关推荐
夜斗不是神 2020-11-17
huavhuahua 2020-11-20
Yasin 2020-11-16
xiaoseyihe 2020-11-16
千锋 2020-11-15
diyanpython 2020-11-12
chunjiekid 2020-11-10
wordmhg 2020-11-06
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16