写在前面
11弹有点难,先跳一跳,又好久没有学这个了呜呜呜
Python Round-12
左对齐控制输出
num = input().split(",")
for i in num:
print("{:<10}".format(i),end="")
Pow函数
scale = 0.0001 # 成就值增量
def calv(base, day):
val = base * pow(1+scale,day*11)
return val
print('5年后的成就值是{}'.format(int(calv(1, 5 * 365))))
year = 1
while calv(1,year*365) < 100:
year += 1
print('{}年后成就值是100'.format(year))
判断循环
while True:
try:
a = eval(input('请输入一个正整数: '))
if a > 0 and (type(a) != float):
print(a)
break
else:
print("请输入正整数")
except:
print("请输入正整数")
这里需要注意的是 (type(a) != float) 我在自己写的时候写成了 a != float,这是不对的(因为会一直为 True ),我们实际想检测的其实是type(a),切记
turtle库
import turtle as t
ls = [69, 292, 33, 131, 61, 254]
X_len = 400
Y_len = 300
x0 = -200
y0 = -100
t.penup()
t.goto(x0, y0)
t.pendown()
t.fd(X_len)
t.fd(-X_len)
t.seth(90)
t.fd(Y_len)
t.pencolor('red')
t.pensize(5)
for i in range(len(ls)):
t.penup()
t.goto(x0 + (i+1)*50,y0)
t.seth(90)
t.pendown()
t.fd(ls[i]) # 不能直接用i,要用ls[i]接收数据
t.done()
查找元素
import random
d = {"BOB":"2112314234","AOB":"213454356","COB":"687564342",}
s = input("请输入想查找的人名:")
if s in d:
i = 0
lst = []
while i < 4:
a = str(random.randint(1, 10))
lst.append(a)
i += 1
aa = "".join(lst)
print("{0} {1} {2}".format(s, d[s],aa))
else:
print("对不起,您输入的用户不存在")
这个是我自己写的,基本能达到效果
注意:使用join方法时,括号内的参数 list 必须只包含 str 类型的成员 这两种方法均是 str 的方法,即.之前必须为 str 类型
参考答案:
import random
random.seed(2)
pdict={'Alice':['123456789'],'Bob':['234567891'],'Lily':['345678912'],'Jane':['456789123']}
name = input('请输入一个人名:')
if name in pdict:
print(name,pdict[name][0],random.randint(1000,9999))
else:
print('对不起,您输入的用户信息不存在。')
确实哈确实,为啥不直接random.randint(1000,9999)呢,再一次被我智商秀到了。。。
综合应用
import jieba
f1 = open("data2018.txt","r")
f2 = open("data2019.txt","r")
txt1 = f1.read()
txt2 = f2.read()
A = ",.!?,。、\n()()《》<>=+-*""“”..."
d1 = {}
d2 = {}
a =jieba.lcut(txt1)
for i in a:
if i in A:
continue
elif len(i)<2:
continue
else:
d1[i] = d1.get(i,0)+1
lt = list(d1.items())
lt.sort(key = lambda x:x[1],reverse = True)
f = []
for qw in lt:
bb = list(qw)
f.append(bb)
n = 0
while n<10:
for ii in f:
print("{0}:{1}".format(ii[0],ii[1]),end=", ")
n += 1
写了一部分,放弃了,感觉总还是有点问题
参考答案:
import jieba
def fenci(txt):
f=open(txt,'r')
datas=f.read()
f.close()
data=jieba.lcut(datas)
d = {}
for i in data:
if len(i)>=2:
d[i]=d.get(i,0)+1
lt = list(d.items())
lt.sort(key = lambda x:x[1],reverse = True)
return lt
def show(lt):
for i in lt[:9]:
print(i[0],':',i[1],end=',',sep='')
print(lt[9][0],':',lt[9][1],sep='')
l1=fenci('data2018.txt')
l2=fenci('data2019.txt')
print('2019:',end='',sep='')
show(l2)
print('2018:',end='',sep='')
show(l1)
#
# 以下代码仅供参考。
#
import jieba
def fenci(txt):
f=open(txt,'r')
datas=f.read()
f.close()
data=jieba.lcut(datas)
d = {}
for i in data:
if len(i)>=2:
d[i]=d.get(i,0)+1
lt = list(d.items())
lt.sort(key = lambda x:x[1],reverse = True)
ls = [x[0] for x in lt[:10]]
return ls
def show(lt):
print(','.join(lt))
l1=fenci('data2018.txt')
l2=fenci('data2019.txt')
l3=[]
for i in l1:
if i in l2:
l3.append(i)
for i in l3:
l1.remove(i)
l2.remove(i)
print('共有词语:',end='',sep='')
show(l3)
print('2019特有:',end='',sep='')
show(l2)
print('2018特有:',end='',sep='')
show(l1)
好多又忘了,慢慢练习慢慢加强