Python学习_06 习题练习
2018-04-10 23:15
330 查看
Python学习_06 习题练习2018-04-031、输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数
代码如下:#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2018\4\10 0010 20:31# @Author : xiexiaolong# @File : 0410.py'''输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数'''while 1: strings = input("Please input a string(quit will be exit):") alpha, dig, space, other = 0, 0, 0, 0 if strings.strip()=="quit": exit(1) for i in strings: if i.isdigit(): dig += 1 elif i.isspace(): space += 1 elif i.isalpha(): alpha += 1 else: other += 1 print("alpha = {0}".format(alpha)) print("dig = {0}".format(dig)) print("space = {0}".format(space)) print("other = {0}".format(other))结果:D:\python\venv\Scripts\python.exe D:/python/0410.pyPlease input a string(quit will be exit):1 2 3 4 !@#$ sfsssssff dddddalpha = 14dig = 4space = 7other = 4Please input a string(quit will be exit):quit
Process finished with exit code 12、ABCD乘9等于DCBA 求A = ? B = ? C=? D=?
代码如下:#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2018\4\10 0010 20:50# @Author : xiexiaolong# @File : 0410-2.py##ABCD乘9等于DCBA A = ? B = ? C=? D=?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 ((1000*A + 100*B + 10*C +D)*9 == (1000*D + 100*C + 10* B +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}*9={4}{5}{6}{7}".format(A,B,C,D,D,C,B,A))结果:D:\python\venv\Scripts\python.exe D:/py
4000
thon/0410-2.pyA = 1B = 0C = 8D = 91089*9=9801
Process finished with exit code 0
3、九宫格
代码如下:#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2018\4\10 0010 21:04# @Author : xiexiaolong# @File : 0410-3.py'''九宫格 --------------- | A | B | C | | D | E | F | | G | H | I | ---------------所有的横竖斜线加起来等于15A : 1-9B : 1-9 除AC : 1-9 除A,BD : 1-9 除A,B,C'''number= list()for i in range(1,10): number.append(i)print(number)count = 1for 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
结果如下:D:\python\venv\Scripts\python.exe D:/python/0410-3.py[1, 2, 3, 4, 5, 6, 7, 8, 9]
第1种方法: --------------- | 2 | 7 | 6 | | 9 | 5 | 1 | | 4 | 3 | 8 | ---------------
第2种方法: --------------- | 2 | 9 | 4 | | 7 | 5 | 3 | | 6 | 1 | 8 | ---------------
第3种方法: --------------- | 4 | 3 | 8 | | 9 | 5 | 1 | | 2 | 7 | 6 | ---------------
第4种方法: --------------- | 4 | 9 | 2 | | 3 | 5 | 7 | | 8 | 1 | 6 | ---------------
第5种方法: --------------- | 6 | 1 | 8 | | 7 | 5 | 3 | | 2 | 9 | 4 | ---------------
第6种方法: --------------- | 6 | 7 | 2 | | 1 | 5 | 9 | | 8 | 3 | 4 | ---------------
第7种方法: --------------- | 8 | 1 | 6 | | 3 | 5 | 7 | | 4 | 9 | 2 | ---------------
第8种方法: --------------- | 8 | 3 | 4 | | 1 | 5 | 9 | | 6 | 7 | 2 | ---------------
Process finished with exit code 0
代码如下:#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2018\4\10 0010 20:31# @Author : xiexiaolong# @File : 0410.py'''输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数'''while 1: strings = input("Please input a string(quit will be exit):") alpha, dig, space, other = 0, 0, 0, 0 if strings.strip()=="quit": exit(1) for i in strings: if i.isdigit(): dig += 1 elif i.isspace(): space += 1 elif i.isalpha(): alpha += 1 else: other += 1 print("alpha = {0}".format(alpha)) print("dig = {0}".format(dig)) print("space = {0}".format(space)) print("other = {0}".format(other))结果:D:\python\venv\Scripts\python.exe D:/python/0410.pyPlease input a string(quit will be exit):1 2 3 4 !@#$ sfsssssff dddddalpha = 14dig = 4space = 7other = 4Please input a string(quit will be exit):quit
Process finished with exit code 12、ABCD乘9等于DCBA 求A = ? B = ? C=? D=?
代码如下:#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2018\4\10 0010 20:50# @Author : xiexiaolong# @File : 0410-2.py##ABCD乘9等于DCBA A = ? B = ? C=? D=?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 ((1000*A + 100*B + 10*C +D)*9 == (1000*D + 100*C + 10* B +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}*9={4}{5}{6}{7}".format(A,B,C,D,D,C,B,A))结果:D:\python\venv\Scripts\python.exe D:/py
4000
thon/0410-2.pyA = 1B = 0C = 8D = 91089*9=9801
Process finished with exit code 0
3、九宫格
代码如下:#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2018\4\10 0010 21:04# @Author : xiexiaolong# @File : 0410-3.py'''九宫格 --------------- | A | B | C | | D | E | F | | G | H | I | ---------------所有的横竖斜线加起来等于15A : 1-9B : 1-9 除AC : 1-9 除A,BD : 1-9 除A,B,C'''number= list()for i in range(1,10): number.append(i)print(number)count = 1for 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
结果如下:D:\python\venv\Scripts\python.exe D:/python/0410-3.py[1, 2, 3, 4, 5, 6, 7, 8, 9]
第1种方法: --------------- | 2 | 7 | 6 | | 9 | 5 | 1 | | 4 | 3 | 8 | ---------------
第2种方法: --------------- | 2 | 9 | 4 | | 7 | 5 | 3 | | 6 | 1 | 8 | ---------------
第3种方法: --------------- | 4 | 3 | 8 | | 9 | 5 | 1 | | 2 | 7 | 6 | ---------------
第4种方法: --------------- | 4 | 9 | 2 | | 3 | 5 | 7 | | 8 | 1 | 6 | ---------------
第5种方法: --------------- | 6 | 1 | 8 | | 7 | 5 | 3 | | 2 | 9 | 4 | ---------------
第6种方法: --------------- | 6 | 7 | 2 | | 1 | 5 | 9 | | 8 | 3 | 4 | ---------------
第7种方法: --------------- | 8 | 1 | 6 | | 3 | 5 | 7 | | 4 | 9 | 2 | ---------------
第8种方法: --------------- | 8 | 3 | 4 | | 1 | 5 | 9 | | 6 | 7 | 2 | ---------------
Process finished with exit code 0
相关文章推荐
- 学习python过程我所练习的一些习题
- Python学习笔记: 第五章课后习题练习
- PythonNLP学习进阶:习题练习(2016-2-12)
- 笨方法学习Python-习题25: 更多更多的练习
- 笨方法学习Python-习题24: 更多练习
- SSH与SSM学习之SSH实现CRM练习06——客户列表06_页面和测试
- 学习python(4) 练习词典功能 查询增加删除更新
- Python:个人学习和练习笔记
- Learn Python The Hard Way学习(28) - 布尔运算练习
- Python学习日志 - 8.函数(习题解答)
- 本方法学python 习题24(综合练习) 习题25(更多关于函数和变量的练习)习题26(修改程序)
- 过滤(s='good ' if m%2==0 else 'bad') 分类: python 小练习 python基础学习 2013-12-27 13:45 220人阅读 评论(0) 收藏
- Python Web开发-Django2.0学习06
- 笨方法学习Python-习题17: 更多文件操作
- PYTHON爬虫学习——urllib库之Debuglog与URLError实战练习
- Python学习练习1---直接上手编程
- python学习笔记十 类文件对象 分类: python基础学习 python 2013-06-18 12:03 254人阅读 评论(0) 收藏
- 笨办法学Python学习笔记 练习18
- 笨方法学习Python-习题20: 函数和文件
- classmethod类方法 分类: python 小练习 python基础学习 2014-02-07 10:36 214人阅读 评论(0) 收藏