Python语言程序设计——第四部分全部
示例程序在课程配套教材《Python语言程序设计基础》83页。稍作修改
- PassInit = input()
- for ind in PassInit:
-
if (ord('a') <= ord(ind) <= ord('z')):
-
PassKaisa = chr(ord('a')+(ord(ind)-ord('a')+3)%26)
-
print(PassKaisa,end='')
-
elif (ord('A') <= ord(ind) <= ord('Z')):
-
PassKaisa = chr(ord('A')+(ord(ind)-ord('A')+3)%26)
-
print(PassKaisa,end='')
-
else:
-
PassKaisa = ind
-
print(PassKaisa,end='')
-
continue
- 三位水仙花数计算
除了计算个位、十位和百位,以及对应输出格式之外,也没有什么难点 - NumofSXH = []
- for ind in range(100,1000):
-
A = ind//100
-
B = (ind//10%10)
-
C = ind%10
-
if pow(A,3) + pow(B,3) + pow(C,3) == ind:
-
NumofSXH.append(ind)
-
else:
-
continue
- LenSXH = len(NumofSXH)
- for ind1 in range(LenSXH):
-
if ind1 < (LenSXH-1):
-
print('{},'.format(NumofSXH[ind1]),end='')
-
else:
-
print('{}'.format(NumofSXH[ind1]))
- 说句心里话
你们都想说什么呢~~ - a = input()
- b = input()
- str1 = a + ‘,我想对你说,’ + b
- print(str1)
- 字符串垂直输出
我在想是否应该可以一行代码搞定的,我可能写复杂了 - a = input()
- for ind in a:
-
print(ind)
- 词频统计《哈姆雷特》
这道题相对来说是代码量比较大的题,自己写有一丢丢的难度,但是与课程上给的实例完全相同,只有一些输出格式上的变化,因此代码有问题的。 - def getText():
-
txt = open('hamlet.txt','r').read()
-
txt = txt.lower()
-
for ch in '!"#$%&()*+,-./:;<>=?@[]\\^_{}|`~':
-
txt = txt.replace(ch," ")
-
return txt
- if name == ‘main’:
-
content = getText()
-
words = content.split()
-
counts = {}
-
for ind in words:
-
counts[ind] = counts.get(ind,0) + 1
-
items = list(counts.items())
-
items.sort(key=lambda x:x[1], reverse=True)
-
for ind1 in range(10):
-
word, times = items[ind1]
-
print('{:<10},{:>5}'.format(word,times))
用到看到的点个赞呗!