五、python的练习题

时间:2023-02-14 20:47:03

1、输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数

#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/6/5 17:53 # @Author : chenjiahe # @File : count.py

#用isdigit函数判断是否数字 #用isalpha判断是否字母 #用isalnum判断是否数字和字母的组合
while 1: strings = input("Please input a strings(quit will exit): ") alpha, dig, space, other = 0, 0, 0, 0 if strings.strip() == "quit": exit(1) for i in strings: if i.isalpha(): alpha += 1
        elif i.isdigit(): dig += 1
        elif i.isspace(): space += 1
        else: other += 1
    print("alpha:{0}".format(alpha)) print("dig:{0}".format(dig)) print("space:{0}".format(space)) print("other:{0}".format(other))

执行结果

五、python的练习题

2、99乘法表

#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/6/5 18:33 # @Author : chenjiahe # @File : 9*9.py

for a in range(1,10): for b in range(1,a+1): print("{0} x {1} = {2} ".format(a,b,a*b),end='  ') print()

结果:

五、python的练习题

3、九宫格(数独)

#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/6/6 16:11 # @Author : chenjiahe # @File : 九宫格.py
''' 九宫格 ------------- | A | B | C | | D | E | F | | G | H | I | ------------- 所有的横竖斜线加起来都等于15 ''' number = [1,2,3,4,5,6,7,8,9] count = 1
for A in number: a = number.copy() a.remove(A) for B in a: b = a.copy() b.remove(B) for C in b: c = b.copy() c.remove(C) for D in c: d = c.copy() d.remove(D) for E in d: e = d.copy() e.remove(E) for F in e: f = e.copy() f.remove(F) for G in f: g = f.copy() g.remove(G) for H in g: h = g.copy() h.remove(H) for I in h: if((A+B+C == D+E+F == G+H+I == A+D+G == B+E+H == C+F+I ==A+E+I == C+E+G == 15)): print(''' 方法{9} ------------------- | {0} | {1} | {2} | | {3} | {4} | {5} | | {6} | {7} | {8} | ------------------- '''.format(A,B,C,D,E,F,G,H,I,count)) count += 1

结果:

五、python的练习题

五、python的练习题

4、ABCD乘九=DCBA,A=? B=? C=? D=?

#ABCD乘九=DCBA,A=? B=? C=? D=? 答案: a=1,b=0,c=8,d=9 1089*9=9801

for a in range(1,10): for b in range(0,10): for c in range(0,10): for d in range(1,10): if ((a*1000 + b*100 + c*10 +d)*9 == (d*1000 +c*100 +b*10 + a)): print("a = {0}".format(a)) print("b = {0}".format(b)) print("c = {0}".format(c)) print("d = {0}".format(d)) print("{0}{1}{2}{3}x9={3}{2}{1}{0}".format(a,b,c,d))

结果:

五、python的练习题

 5、阶乘