本文共 2008 字,大约阅读时间需要 6 分钟。
while 1: number = char = space = other = 0 strings = input("Please inpur a string(quit will be exit):") if strings == 'quit' or strings == 'exit': exit(1) for i in strings: if i.isdigit(): number+=1 elif i.isalpha(): char+=1 elif i.isspace(): space+=1 else: other+=1 print('number={0}\nchar={1}\nspace={2}\nother={3}'.format(number,char,space,other))
练习2:
ABCD乘9=DCBA,A=? B=? C=? D=? 答案:a=1,b=0,c=8,d=9 1089*9=9801for A in range(1,10):
for B in range(0,10):for C in range(0,10):for D in range(1,10):if A1000+B100+C10+D==(D1000+C100+B10+A)9:print('{0}{1}{2}{3}9={4}{5}{6}{7}'.format(A,B,C,D,D,C,B,A))九宫格练习3:
number=list()count=1for i in range(10): number.append(i)num=[(a,b,c) for a in number for b in number if a!=b for c in number if a!=c and b!=c and a+b+c==15]for L1 in num: for L2 in num: if set(L1) & set(L2): continue for L3 in num: if set(L1) & set(L2) & set(L3): continue if L1[0]+L2[0]+L3[0]!=15: continue elif L1[1]+L2[1]+L3[1]!=15: continue elif L1[2]+L2[2]+L3[2]!=15: continue elif L1[0]+L2[1]+L3[2]!=15: continue elif L1[2]+L2[1]+L3[0]!=15: continue else: print(''' 第{9}种例子 ------------- | {0} | {1} | {2} | | {3} | {4} | {5} | | {6} | {7} | {8} | -------------'''.format(L1[0], L1[1], L1[2], L2[0], L2[1], L2[2], L3[0], L3[1], L3[2], count)) count += 1
只有在python2中才有编码问题
在python2默认编码是ASCII, python3里默认是unicodeunicode 分为 utf-32(占4个字节),utf-16(占两个字节),utf-8(占1-4个字节), so utf-16就是现在最常用的unicode版本, 不过在文件里存的还是utf-8,因为utf8省空间转载于:https://blog.51cto.com/jacksoner/2064857