一、:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:利用while语句,条件为输入的字符不为’\n’.
#用isdigit函数判断是否数字
#用isalpha判断是否字母
#isalnum判断是否数字和字母的组合
程序如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 4/3/2018 11:50 AM
# @Author : zhdya
# @File : demon5.py
i_str = 0
sum = 0
i_num = 0
i_spa = 0
i_spe = 0
while 1:
aa = input("pls input sth you like: ")
if aa == "\\n":
break
else:
for i in aa:
if i.isalpha():
i_str += 1
elif i.isdigit():
i_num += 1
elif i.isspace():
i_spa += 1
else :
i_spe += 1
print("the number of charcter is %d" %i_str)
print("the number of number is %d" %i_num)
print("the number of space is %d" %i_spa)
print("the number of special worlds is %d" %i_spe)
二、随机输入一个数字,计算这个数字的阶乘和
注意判断:
- 如果用户输入非法字符,需要提示
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 4/3/2018 11:50 AM
# @Author : zhdya
# @File : demon5.py
def jc(n):
sum = 1
if n == 0:
return sum
else:
for i in range(1, int(n)+1):
sum *= i
return sum
sumt = 0
number = input("pls input a number: ")
if number.isdigit():
for i in range(0, int(number)+1):
sumt += jc(i)
print("the {0} factorial is {1}".format(number, sumt))
elif number.isalpha():
print("pls just input a number!!")
else:
print("pls just input a number!!")
三、ABCD乘9=DCBA,A=? B=? C=? D=?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 4/11/2018 9:16 PM
# @Author : zhdyaa
# @File : demon4.py
'''
## ABCD乘9=DCBA,A=? B=? C=? D=?
## 思路:
## A肯定不能等于2 一旦A等于2 就会出现5位数,不符合要求,所以A只能等于1,从而D等于9。
'''
for A in [1]:
for B in range(0,10):
for C in range(0,10):
for D in [9]:
if (A * 1000 + B * 100 + C * 10 + D) * 9 == (D * 1000 + C * 100 + B * 10 + A):
print("A is {0}".format(A))
print("B is {0}".format(B))
print("C is {0}".format(C))
print("D is {0}".format(D))
print("{0}{1}{2}{3}*9={3}{2}{1}{0}".format(A,B,C,D))
四、九宫格
'''
## 九宫格
## -------------
## | A | B | C |
## | D | E | F |
## | G | H | I |
## -------------
## 思路:
## A + B + C = A + D + G = G + H + I = C + F + I = A + E + I = G + E + C
## 9个数字均为唯一的;当A = 1;其它字母就不可以为1;同理去筛选已经被占用的数字
## 所有的横竖斜线加起来都等于15
'''
number = list()
for i in range (1,10):
number.append(i)
print(number)
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 == A + D + G == G + H + I == C + F + I == A + E + I == G + E + C == 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
支持中文的编码:utf-8, gbk, gb2312
decode 解码
encode 编码
原有编码 -> unicode编码 -> 目的编码
decode("utf-8") 解码 -> unicode -> encode("gbk") 编码
不写python代码排头,就会报错。
s = "哈哈哈"
print(s)
这个代码文件被执行时就会出错,就是编码出了问题。
python默认将代码文件内容当作asci编码处理,但asci编码中不存在中文,因此抛出异常。
就是要让python知道文件中使用的是什么编码形式,对于中文,可以用的常见编码有utf-8,gbk和gb2312等。只需在代码文件的最前端添加如下:
#-*- coding:utf-8 -*-
说明:
文件申明是utf-8的编码,识别“哈哈”以后,以unicode对象的形式存在。
如果我们用type查看,存储形式是unicode,python在向控制台输出unicode对象的时候会自动根据输出环境的编码进行转换。
如果输出的不是unicode对象而是str类型。则会按照字符串的编码输出字符串。
从而出现utf8没法在gbk编码的控制台展现。
s = "哈哈哈"
m = s.decode("utf-8")
print(type(m))
print(m)