異常和模組-1
阿新 • • 發佈:2020-10-05
1.輸入一行字元,分別統計出其中的數字、字母、空行和其他字元的個數;
#encoding = utf-8 def get_result(): command = input("enter the lines:") digits_arr,alpha_arr,space_arr,others_arr = [],[],[],[] for i in command: if i.isdigit(): digits_arr.append(i) elif i.isalpha(): alpha_arr.append(i)elif i.isspace(): space_arr.append(i) else: others_arr.append(i) return len(digits_arr),len(alpha_arr),len(space_arr),len(others_arr) print(get_result())
2.求 s= a+aa+aaa+aaaa+aa....aa的s的值,其中a是 一個數字
#encoding = utf-8 def get_result(a,n): res = 0for i in range(1,n): res += int(str(a) * i) return res print(get_result(5,6))
3.列印檔案a.txt的檔案路徑
#encoding = utf-8 def get_result(path): res = [] import os for root,dirs,files in os.walk(path): for file in files: if file == "a.txt": res.append(os.path.join(root,file))return res print(get_result(r"D:\workspace"))
4.求一個檔案路徑下所有檔案的數量
#encoding = utf-8 def get_result(): path = r"D:\workspace\py" import os os.chdir(path) res = [] for file in os.listdir(): if os.path.isfile(os.path.join(path,file)): res.append(file) return len(res) print(get_result())
5.找出txt檔案的個數
#encoding = utf-8 def get_result(): path = r"D:\workspace\py" import os os.chdir(path) res = [] for file in os.listdir(): if os.path.isfile(os.path.join(path,file)): if os.path.splitext(file)[1] == ".txt": res.append(file) return len(res) print(get_result())
6.檢視目錄下的所有檔案
#encoding = utf-8 def get_result(): path = r"D:\workspace\py" import os os.chdir(path) for file in os.listdir(): if os.path.isfile(os.path.join(path,file)): print(file) print(get_result())
7.找出一個目錄及子目錄下的所有檔名字,不含字尾
#encoding = utf-8 def get_result(): path = r"D:\workspace\py" import os res = [] for root,dirs,files in os.walk(path): for file in files: if os.path.isfile(os.path.join(root,file)): file_name = os.path.splitext(file)[0] res.append(file_name) return res print(get_result())
8.輸入一個字母判斷是星期幾,如果第一個字母相同就判斷第二個字
#encoding = utf-8 def get_result(): weeks = ["Monday","Tuesday","Wednesday","Thursday", "Friday","Saturday","Sunday"] while 1: first_command = input("enter the first letter:") if first_command == ".": print("bye") break tmp = [] for i in weeks: if first_command.lower() == i[0].lower(): tmp.append(i) if tmp: if len(tmp) == 1: print(tmp[0]) if len(tmp) > 1: second_command = input("enter the second letter:") for j in tmp: if second_command.lower() == j[1].lower(): print(j) else: print("輸入指令不在查詢範圍,請重新輸入") get_result()
8.兩個 3 行 3列的矩陣,實現其對應位置的資料相加,並返回一個矩陣
#encoding = utf-8 def get_result(x,y): res = [[0,0,0] for i in range(3)] for i in range(len(x)): for j in range(len(y)): res[i][j] = x[i][j] + y[i][j] return res x = [[12,7,3],[4,5,6],[7,8,9]] y = [[5,8,1],[6,7,3],[4,5,9]] print(get_result(x,y))
9.把所有的輸入引數中的字母個數統計出來
#encoding = utf-8 def get_result(): res_count = 0 command = input("enter the command:") for i in command: if (i >= "a" and i <= "z") or (i >= "A" and i <= "Z"): res_count += 1 return "字母個數:{}".format(res_count) print(get_result())
10.你輸入的引數全部都是數字,計算所有數字引數的和
#encoding = utf-8 def get_result(): command = input("enter the command:") return sum([int(x) for x in command]) print(get_result())
11.輸入一個數字,如果是數字退出,不是數字讓使用者繼續輸入
#encoding = utf-8 def get_result(): while 1: command = input("enter the command:") if command.isdigit(): print("bye") break get_result()
12.程式碼行統計工具
#encoding = utf-8 def get_result(): import os path = r"D:\workspace\file\delete" os.chdir(path) res = [] for i in os.listdir(): if os.path.isfile(os.path.join(path,i)): if os.path.splitext(i)[1] in [ ".java",".php",".py"]: with open(i,encoding = "utf-8") as fp1: for line in fp1: if line: if line[0] not in "\n\v\r\f\t": res.append(line) return len(res) print(get_result())