題目:輸入一行字符串,分別統計出其中英文字母、空格、數字和其它字符的個數。python
1.程序分析:利用while語句,條件爲輸入的字符不爲'\n'. #用isdigit函數判斷是否數字 #用isalpha判斷是否字母 #isalnum判斷是否數字和字母的組合 。git
#!/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
題目:寫出代碼實現輸入一個數字,能夠自動計算這個數的階乘。ui
亦即n!=1×2×3×...×n。階乘亦能夠遞歸方式定義:0!=1,n!=(n-1)!×n。編碼
如:spa
0! = 1
1! = 1
2! = 2*1=2
3! = 3*2*1=6code
程序代碼:orm
#!/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
題目: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
題目:寫出代碼實現九宮格數獨的計算。
-------------
| 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 | -------------
1.utf-8 Pycharm程序中和cmd中都不會出現亂碼。
2.當爲gbk時都有沒亂碼
三、文件申明是utf-8的編碼,識別「你好」之後,以unicode對象的形式存在。若是咱們用type查看,存儲形式是unicode,python在向控制檯輸出unicode對象的時候會自動根據輸出環境的編碼進行轉換。若是輸出的不是unicode對象而是str類型。則會按照字符串的編碼輸出字符串。從而出現utf8無法在gbk編碼的控制檯展示
四、列表中Python2中控制檯出現亂碼,cmd中不會出現亂碼,python3中都無亂碼:
python3中:
五、編碼轉換報錯:python2中有控制檯有亂碼,cmd中沒有亂碼,python3中均無亂碼:
python3: