1.排序输出字典中数据
dic1 = {'Tom':21,'Bob':18,'Jack':23,'Ana':20}
dic2 = {'李雷':21,'韩梅梅':18,'小明':23,'小红':20}
n=int(input())
if n>len(dic1):
n=len(dic1)
print(sorted(dic1.keys())[:n])
print(sorted(dic2.items(),key=lambda item:item[1])[:n])
2.罗马数字转换
def F(s):
d={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,
'M':1000,'IV':3,'IX':8,'XL':30,'XC':80,'CD':300,'CM':800}
t=0
for i in range(len(s)):
if s[i-1:i+1] in d:
t+=d[s[i-1:i+1]]
else:
t+=d[s[i]]
return t
s=input()
print(F(s))
3.模拟用户登录
username in
user_table={'Kate':'xxy123','RRAj':'45desx','admin':'ie1234','Rose':'89dkyx12'}
c=0
while c<3:
username=input()
password=input()
if username in user_table.keys() and password==user_table[username]: # username 用in
print("登录成功")
break
else:
print('登录失败,请重新输入!')
4.特殊回文数
#逆序
def R(s):
return int(str(s)[::-1])
#求和
def S(s):
return sum([int(i) for i in str(s)])
n=int(input())
flag=0
if n<1 or n>54:
print("输入错误,请重新输入!")
else:
for j in range(10000,1000000):
if R(j)==j and S(j)==n:
print(j)
flag=1
if flag==0:
print("无满足条件的数!")
5.简明英汉字典
dic={}
with open('dict.txt','r',encoding='utf-8') as d:
for x in d:
x=x.strip().split()
dic.update({x[0]:x[1]})
word=input()
print(word,dic.get(word,'这个词我不明白'))