一、猜字遊戲

#猜字遊戲(從1到10)
import easygui as g
import random g.msgbox('歡迎進入探險之路!')
screct = random.randint(1,10) msg = '猜一下塵封的幸運數字是那個'
title = '數字探險之路'
guess = g.integerbox(msg,title,lowerbound=1,upperbound=10) while 1:
if guess == screct:
g.msgbox('很牛啊!')
g.msgbox('猜對有獎勵啊')
break
else:
if guess > screct:
g.msgbox('兄弟猜的大了!')
else:
g.msgbox('兄弟猜的有點小!') guess = g.integerbox(msg,title,lowerbound=1,upperbound=10) g.msgbox('遊戲結束,不玩啦!')

二、實現一個等級使用者資訊的頁面(帶*號必填,要求一定要有輸入並且不能為空格)

'''
|--實現一個等級使用者資訊的頁面(帶*號必填,要求一定要有輸入並且不能為空格)
|--要求使用者名稱,真實姓名,手機號碼,E-mail必填,固定電話,QQ非必填
'''
import easygui as g msg = '輸入以下使用者資訊'
title = '賬戶中心'
fileNames = ['*使用者名稱','*真實姓名','固定電話','*手機號碼','QQ','*E-mail']
fileValues = []
fileValues = g.multenterbox(msg,title,fileNames) while 1:
if fileValues == None:
break
errmsg = "" for i in range(len(fileNames)):
option = fileNames[i].strip()
if fileValues[i].strip() == "" and option[0] =="*":
errmsg += ('【%s】為必填項。\n\n' %fileNames[i]) if errmsg == "":
break
fileValues = g.multenterbox(errmsg,title,fileNames,fileValues) print('使用者資料如下:%s'% str(fileValues))

三、提供一個檔案瀏覽框,讓使用者選擇需要開啟的文字檔案,開啟並顯示檔案內容

'''
|--開啟使用者選擇的檔案,顯示文字內容
|--比較檔案是否修改過,如果修改過,則提示:覆蓋儲存,不儲存,另存外
'''
import easygui as g
import os file_path = g.fileopenbox(default='/Users/yixia/Desktop/*.txt') with open(file_path) as old_file:
title = os.path.basename(file_path)
msg = '檔案【%s】的內容如下:'%title
text = old_file.read() text_after=g.textbox(msg,title,text) if text != text_after[:-1]:
choice = g.choicebox('檢測到文字內容發生了變化,請做如下修改','警告',('覆蓋儲存','不儲存','另存為...')) if choice == "覆蓋儲存":
with open(file_path,'w') as old_file:
old_file.write(text_after[:-1])
if choice == "不儲存":
pass
if choice == "另存為...":
another_path = g.filesavebox(default='/Users/yixia/Desktop/*.txt')
if os.path.splitext(another_path)[1]!="*.txt":
another_path += '.txt' with open(another_path,'w') as new_file:
new_file.write(text_after[:-1])

四、4. 寫一個程式統計你當前程式碼量的總和,並顯示離十萬行程式碼量還有多遠??-D+=mG
RN'o@:cu(O5=&?{t2q>!SK[|<;QPV

    • 要求一:遞迴搜尋各個資料夾
    • 要求二:顯示各個型別的原始檔和原始碼數量
    • 要求三:顯示總行數與百分比
import easygui as g
import os #此方法用於展示結果
def show_result(start_dir):
lines = 0
total = 0
text = "" #遍歷這個檔案內的原始檔,輸入最終結果:什麼檔案多少個程式碼多少行
for i in source_list:
#統計每一個檔案的程式碼行
lines = source_list[i]
#統計多少行程式碼
total += lines
text += "【%s】原始檔 %d 個,原始碼 %d 行\n" % (i, file_list[i], lines)
title = '統計結果'
msg = '您目前共累積編寫了 %d 行程式碼,完成進度:%.2f %%\n離 10 萬行程式碼還差 %d 行,請繼續努力!' % (total, total / 1000, 100000 - total)
g.textbox(msg, title, text) #統計每個檔案有多少行程式碼
def calc_code(file_name):
lines = 0
with open(file_name) as f:
print('正在分析檔案:%s ...' % file_name)
try:
for each_line in f:
lines += 1
except UnicodeDecodeError:
pass # 不可避免會遇到格式不相容的檔案,這裡忽略掉......
return lines #開啟資料夾查詢檔案
def search_file(start_dir):
#改變當前工作目錄到指定的路徑
os.chdir(start_dir) #os.listdir(os.curdir):列舉當前目錄的檔名,遍歷所有檔案
for each_file in os.listdir(os.curdir):
#已字尾分割檔案,放在ext文字內
ext = os.path.splitext(each_file)[1]
if ext in target:
lines = calc_code(each_file) # 統計行數
# 還記得異常的用法嗎?如果字典中不存,丟擲 KeyError,則新增字典鍵
# 統計檔案數
try:
file_list[ext] += 1
except KeyError:
file_list[ext] = 1
# 統計原始碼行數
try:
source_list[ext] += lines
except KeyError:
source_list[ext] = lines
#判斷是否存在查詢檔案的一個目錄
if os.path.isdir(each_file):
search_file(each_file) # 遞迴呼叫
os.chdir(os.pardir) # 遞迴呼叫後切記返回上一層目錄 target = ['.c', '.cpp', '.py', '.cc', '.java', '.pas', '.asm']
file_list = {}
source_list = {} g.msgbox("請開啟您存放所有程式碼的資料夾......", "統計程式碼量")
path = g.diropenbox("請選擇您的程式碼庫:") search_file(path)
show_result(path)