1.实例一:
题目:输入一行字符串,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:利用while语句,条件为输入的字符不为'\n'. #用isdigit函数判断是否数字 #用isalpha判断是否字母 #isalnum判断是否数字和字母的组合 。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/4/10 22:13
# @Author : Feng Xiaoqing
# @File : string.py
# @Function: -----------
num_dig=0
num_alph=0
num_space=0
num_other=0
while 1:
strings=input("Please input string:")
if strings=="quit":
exit(0)
for i in strings:
if i.strip().isdigit():
num_dig += 1
elif i.strip().isalpha():
num_alph += 1
elif i.isspace():
num_space += 1
else:
num_other += 1
print("数字个数为:{0}".format(num_dig))
print("字母个数为:{0}".format(num_alph))
print("空格个数为:{0}".format(num_space))
print("其他字符个数为:{0}".format(num_other))
运行结果:
Please input string:kadsf83543k#### asdflka
数字个数为:5
字母个数为:13
空格个数为:1
其他字符个数为:4
2.实例二:
题目:写出代码实现输入一个数字,可以自动计算这个数的阶乘。
亦即n!=1×2×3×...×n。阶乘亦可以递归方式定义:0!=1,n!=(n-1)!×n。
如:
0! = 1
1! = 1
2! = 2*1=2
3! = 3*2*1=6
程序代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/4/11 22:48
# @Author : Feng Xiaoqing
# @File : 4.JieCheng_for_n.py
# @Function: ----------
n=input("Please input a number n: ")
result = 1
num = 1
for i in range(1,int(n)+1):
num *=i
result += num
print("{0}的阶乘为:{1}".format(n,result))
另一种:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/4/10 21:59
# @Author : Feng Xiaoqing
# @File : jiecheng_for_n.py
# @Function: -----------
def jc(n):
result = 1
if n == 0:
return result
else:
for i in range(1,n+1):
result *= i
return result
n = input("Please input number n:")
count = 0
for i in range(0,int(n)+1):
count += jc(i)
print("count = {0}".format(count))
运算结果:
Please input a number n: 8
8的阶乘为:46234
另一种:
Please input number n:8
count = 46234
3.实例三:
题目:ABCD乘9=DCBA,A=? B=? C=? D=? 计算A、B、C、D的值分别为多少?
答案:a=1,b=0,c=8,d=9 1089*9=9801
程序代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/4/11 22:14
# @Author : Feng Xiaoqing
# @File : 2.ABCDX9=DCBA.py
# @Function: -----------
for A in range(1,10):
for B in range(0,10):
for C in range(0,10):
for D in range(0,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))
运行结果:
A=1
B=0
C=8
D=9
1089x9=9801
4.实例四:
题目:写出代码实现九宫格数独的计算。
-------------
| A | B | C |
| D | E | F |
| G | H | I |
-------------
A,B,C,D,E,F,G,H,I 取值为1-9
所有的横竖斜线加起来都等于15:
(A+B+C)=(D+E+F)=(G+H+I)=(A+D+G)=(B+E+H)=(C+F+I)=(A+E+I)=(G+E+C)= 15
程序代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/4/11 23:31
# @Author : Feng Xiaoqing
# @File : jiu_Gong_Ge.py
# @Function: -----------
count=0
list=[0,1,2,3,4,5,6,7,8,9]
for A in list:
a=list.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)==(G+E+C)== 15:
count += 1
print("""
---第{9}种----
| {0} | {1} | {2} |
| {3} | {4} | {5} |
| {6} | {7} | {8} |
-------------
""".format(A,B,C,D,E,F,G,H,I,count))
运行结果:
---第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 |
-------------
python中的编码问题:
1.utf-8 Pycharm程序中和cmd中都不会出现乱码。
2.当为gbk时都有没乱码
3、文件申明是utf-8的编码,识别“你好”以后,以unicode对象的形式存在。如果我们用type查看,存储形式是unicode,python在向控制台输出unicode对象的时候会自动根据输出环境的编码进行转换。如果输出的不是unicode对象而是str类型。则会按照字符串的编码输出字符串。从而出现utf8没法在gbk编码的控制台展现
4、列表中Python2中控制台出现乱码,cmd中不会出现乱码,python3中都无乱码:
python3中:
5、编码转换报错:python2中有控制台有乱码,cmd中没有乱码,python3中均无乱码:
python3: