'''
判断一个字符串数字有多少个
字母有多少个
空格有多少个
其他自由有多少个
'''
while 1:
string = input("Please input a string(quit will be exit): ")
alpha, dig, space, other = 0, 0, 0, 0
if string.strip() == "quit":
exit(1)
for i in string:
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))
'''
ABCD乘以9=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 (( 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}X9 = {4}{5}{6}{7} ".format(A,B,C,D,D,C,B,A))
'''
-------------
| A | B | C |
-------------
| D | E | F |
-------------
| G | H | I |
-------------
所有横竖斜线加起来都等于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 == 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
缩进
if ():
while 1:
for
def
class
'''
0! + 1! + 2! +3! + 4! + 5! + n!
1 + 1 +2 +6 +n*(n-1)*(n-2)*(n-3)...*1
'''
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))
python的编码
编码
支持中文的编码:utf-8,gbk,gb2312
decode 解码
encode 编码
不写python代码排头,就会报错
判断一个字符串数字有多少个
字母有多少个
空格有多少个
其他自由有多少个
'''
while 1:
string = input("Please input a string(quit will be exit): ")
alpha, dig, space, other = 0, 0, 0, 0
if string.strip() == "quit":
exit(1)
for i in string:
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))
'''
ABCD乘以9=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 (( 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}X9 = {4}{5}{6}{7} ".format(A,B,C,D,D,C,B,A))
'''
-------------
| A | B | C |
-------------
| D | E | F |
-------------
| G | H | I |
-------------
所有横竖斜线加起来都等于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 == 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
缩进
if ():
while 1:
for
def
class
'''
0! + 1! + 2! +3! + 4! + 5! + n!
1 + 1 +2 +6 +n*(n-1)*(n-2)*(n-3)...*1
'''
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))
python的编码
编码
支持中文的编码:utf-8,gbk,gb2312
decode 解码
encode 编码
不写python代码排头,就会报错