前言

最近在上Python课。简单记录一下自己这些习题的答案。

正文

7-1 密码校验

1
2
3
4
5
passwd = input()
if passwd == "654321":
print("欢迎使用本系统!")
else:
print("密码错误,退出系统!")

7-2 判断闰年

1
2
3
4
5
6
in_n = input()
n = int(in_n)
if ((n % 4 == 0) and (n % 100 != 0)) or (n % 400 == 0) :
print(str(in_n) + "年是闰年。")
else:
print(str(in_n) + "年不是闰年。")

7-3 字符判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
inp = input()
if len(inp) > 1:
print("输入字符个数有误!")
else:
if inp.isdigit():
print("%s是%s。" % (inp,"数字"))
elif inp.isupper():
print("%s是%s。" % (inp,"大写英文字母"))
elif inp.islower():
print("%s是%s。" % (inp,"小写英文字母"))
else:
if inp[2:].isdigit():
print("%s是%s。" % (inp,"数字"))
else:
print("%s是%s。" % (inp,"其他字符"))

7-4 百分制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
inp = input()
iinp = eval(inp)
if (iinp >= 90 and iinp <= 100):
print("%s对应的等级为%s。" % (inp, "优秀"))
elif (iinp >= 80 and iinp < 90):
print("%s对应的等级为%s。" % (inp, "良好"))
elif (iinp >= 70 and iinp < 80):
print("%s对应的等级为%s。" % (inp, "中等"))
elif (iinp >= 60 and iinp < 70):
print("%s对应的等级为%s。" % (inp, "及格"))
elif (iinp < 60 and iinp >= 0):
print("%s对应的等级为%s。" % (inp, "不及格"))
else:
print("%s对应的等级为%s。" % (inp, "无,输入错误"))

7-5 遍历

1
2
3
inp=input()
for c in inp:
print(c)

7-6 飞花令

1
2
3
4
5
6
keyw = input()
src = input()
if keyw in src:
print("恭喜!您的回答正确,包含%s个'%s'。" % (str(src.count(keyw)), keyw))
else:
print("很遗憾!您的回答有误!")

7-7 累加

1
2
3
4
5
6
7
8
9
10
inp = input()
total = 0
print("1+2+3+...+",end="")
for i in range(1,int(inp)+1):
if i!=int(inp):
total += i
else:
print(str(i) + "=",end="")
total += i
print(total)

7-8 吃桃

1
2
3
4
5
6
7
8
9
10
def do_it(src):
res = (src+1) * 2
return res
prog=1
for i in range(9,0,-1):
prog = do_it(prog)
if i != 1:
print("第%s天剩下的%s个桃子" % (str(i), str(prog)))
else:
print("第%s天剩下的%s个桃子" % (str(i), str(prog)),end="")

7-9 累加变体

1
2
3
4
5
6
7
8
9
10
inp = input()
total = 0
for i in range(1,int(inp)+1):
if i!=int(inp):
print(str(i) + "+",end="")
total += i
else:
print(inp + " = ",end="")
total += i
print(total)

7-10 水仙花

1
2
3
4
5
6
7
8
target = input()
sum = 0
for c in target:
sum += int(c) ** 3
if sum == int(target):
print(target + " 是水仙花数。")
else:
print(target + " 不是水仙花数。")

7-11 水仙花变体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
target = input()
sum = 0
if not target.isdigit():
print("输入错误,必须输入三位数字。")
else:
if len(target) != 3:
print("输入错误,必须输入三位。")
else:
for c in target:
sum += int(c) ** 3
if sum == int(target):
print(target + " 是水仙花数。")
else:
print(target + " 不是水仙花数。")

7-12 吃桃变体

1
2
3
4
5
6
7
8
9
10
11
def do_it(src):
res = (src+1) * 2
return res
prog=1
for i in range(9,0,-1):
prog = do_it(prog)
if i != 1:
pass
# print("第%s天剩下的%s个桃子" % (str(i), str(prog)))
else:
print("第%s天剩下的%s个桃子" % (str(i), str(prog)),end="")

7-13 吃桃变体2

1
2
3
4
5
6
7
8
9
10
11
12
13
def do_it(src):
res = (src+1) * 2
return res
days,prog=input().split(",")
days=int(days)
prog=int(prog)
for i in range(days-1,0,-1):
prog = do_it(prog)
if i != 1:
pass
# print("第%s天剩下的%s个桃子" % (str(i), str(prog)))
else:
print("第%s天摘了%s个桃子" % (str(i), str(prog)))

7-14 校验码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
idn = input()
if len(idn) != 17:
print("身份证号码位数有误!")
else:
indexes = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
respective = list(idn)
sum = 0
for i in range(len(respective)):
sum += int(respective[i]) * indexes[i]
modu = sum % 11
id_map = {
0:'1',
1:'0',
2:'X',
3:'9',
4:'8',
5:'7',
6:'6',
7:'5',
8:'4',
9:'3',
10:'2'
}
res = id_map.get(modu)
print("校验码为 " + res)
print("完整的身份证号码为 " + idn +res, end="")

7-15 进制转换

1
2
3
4
5
6
7
8
9
10
11
12
ipt,ipf = input().split(",")
r=int(ipf)
m=int(ipt)
t=''
while m!=0 and r!=0:
c = m % r
if c>9:
t=chr(c-10+65)+t
else:
t=str(c)+t
m=m//r
print("将{}转换为{}进制的结果为:{}".format(ipt,r,t))

7-16 水仙花最终变体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
nacrissa = []

def do_nacrissus(target):
sum = 0
for c in target:
sum += int(c) ** 3
if sum == int(target):
nacrissa.append(target)
return True
return False

for i in range(100,999+1):
do_nacrissus(str(i))

for nacrissus in nacrissa:
print(nacrissus + " ",end="")