Python初級案例教學(xué)【第二課】(Python 黑客對講機(jī),模擬個人用戶登錄,銀行金額大寫漢字轉(zhuǎn)換)
Python模擬個人用戶登錄
Python銀行金額大寫漢字轉(zhuǎn)換
業(yè)務(wù)需求:
示例:
關(guān)鍵技術(shù)分析:
編程思路:
Python模擬個人用戶登錄
Python銀行金額大寫漢字轉(zhuǎn)換
業(yè)務(wù)需求:
示例:
關(guān)鍵技術(shù)分析:
編程思路:
Python競猜商品價(jià)格
編寫一個函數(shù),將黑客精英發(fā)送的信息轉(zhuǎn)換為暗語輸出
總結(jié):
Python模擬個人用戶登錄
業(yè)務(wù)需求:
要求:賬號:admin 密碼:123
1.登錄時(shí)給3次機(jī)會。
2. 如果成功,顯示歡迎xxx。
3. 如果登錄失敗,顯示錄入錯誤你還有x次機(jī)會。如果3次機(jī)會使用完畢,則顯示登錄超限,請明天再登錄。
關(guān)鍵技術(shù)分析:
? 1. 登錄需要用戶名和密碼,也就是兩個字符串。
? 2. 用戶名和密碼應(yīng)該使用鍵盤輸入,獲取兩個字符串。
? 3. 怎么樣才算登錄成功?需要注冊的時(shí)候所使用的用戶名和密碼。
? 4. 驗(yàn)證輸入的用戶名和密碼和注冊時(shí)是否一致。
? 5. 3次機(jī)會,使用for
? 6. 如果登錄成功需要跳出循環(huán)。顯示歡迎xxx。如果失敗,需要if判斷是否機(jī)會使用完畢
for i in range(3): name = input('請輸入用戶名:') pwd = input('請輸入密碼:') if name == 'admin' and pwd == '123': print('歡迎:'+name) break elif i < 2: n = int(2-i) print('輸入錯誤,你還有%d次機(jī)會' % n) continue else: print('登錄超限,請明天再登錄') break
Python銀行金額大寫漢字轉(zhuǎn)換
業(yè)務(wù)需求:
銀行電子支票業(yè)務(wù)在金額部分需要使用大寫的漢字,因此需要將用戶錄入的數(shù)字信息轉(zhuǎn)變?yōu)闈h字。
? 目前只需完成1~5位整數(shù)轉(zhuǎn)換即可。
示例:
輸入金額:> 32542
漢字轉(zhuǎn)換:> 叁 萬 貳 仟 伍 佰 肆 拾 貳 圓 整
關(guān)鍵技術(shù)分析:
? 使用For循環(huán)完成數(shù)字每一位的拆解。
? 利用列表下標(biāo)實(shí)現(xiàn)對位轉(zhuǎn)換。
編程思路:
程序可以拆分為3個環(huán)節(jié)實(shí)現(xiàn):
需要創(chuàng)建兩個列表,為后續(xù)對位轉(zhuǎn)換做準(zhǔn)備:
環(huán)節(jié)1:計(jì)算出用戶輸入金額的位數(shù);
環(huán)節(jié)2:利用已知位數(shù)完成每一位的拆解;
環(huán)節(jié)3:通過列表下標(biāo)對位實(shí)現(xiàn)最終輸出。
? 開發(fā)技巧:
需要創(chuàng)建兩個列表,為后續(xù)對位轉(zhuǎn)換做準(zhǔn)備:
? 漢字列表:[‘零’, ‘壹’, ‘貳’, ‘叁’, ‘肆’, ‘伍’, ‘陸’, ‘柒’, ‘捌’, ‘玖’, ‘拾’]
? 單位列表:[‘圓’,‘拾’, ‘佰’, ‘仟’, ‘萬’]
list_chinese = ['零', '壹', '貳', '叁', '肆', '伍', '陸', '柒', '捌', '玖', '拾'] list_unit = ['圓', '拾', '佰', '仟', '萬'] money = input('input 金額 五位以下: ') price = int(money[:5]) # 去除首位的0 list_price = list(str(price)) end_zero = 1 # 末尾是否為0 now = 1 # 當(dāng)前是否為0 len_price = len(list_price) for i in range(len_price): list_price[i] = list_chinese[int(list_price[i])] # 對位轉(zhuǎn)換成大寫 zero = list_chinese[0] # 零 if list_price[-1] == zero: end_zero = 0 for i in range(len_price): if len_price == 1 and end_zero == 0: print(list_price[0], end='') print(list_unit[0], end='') # 0時(shí) break elif i == len_price - 1 and end_zero == 0: print(list_unit[0], end='') break elif i == len_price - 1 and end_zero == 1: print(list_price[i], end='') print(list_unit[len_price - i - 1], end='') else: if list_price[i] == zero: now = 0 # 當(dāng)前為0 else: now = 1 if now == 1 or (now == 0 and list_price[i - 1] != zero and end_zero == 1): print(list_price[i], end='') if now == 1 and i != len_price - 1: # 若當(dāng)前不為0 print(list_unit[len_price - i - 1], end='') print('整')
Python競猜商品價(jià)格
import random n = random.randint(1, 600) # print(n) for i in range(10): m = int(input('請輸入競猜價(jià)格:')) if m < n: print('價(jià)格低了,請繼續(xù)') elif m > n: print('價(jià)格高了,請繼續(xù)') else: print('你真厲害,猜對了!') break if i == 9: print('本次競猜失敗,請下次努力')
編寫一個函數(shù),將黑客精英發(fā)送的信息轉(zhuǎn)換為暗語輸出
如發(fā)送的信息中含有數(shù)字0,就把數(shù)字 0替換為暗語字母 O 。含有數(shù)字2, 2替換為暗語學(xué)母 Z
def jiami(list1, list2): list3 = [] for i in list1: list3.append(list2[int(i)]) return list3 if __name__ == '__main__': lis1 = input('請輸入一組數(shù)字信息:') lis2 = ['O', 'I', 'Z', 'E', 'Y', 'S', 'G', 'L', 'B', 'P'] n = jiami(lis1, lis2) print(n)
總結(jié):
通過這幾個案例對Python有了更加深刻地理解!
5G教育 Python 數(shù)據(jù)結(jié)構(gòu) 匯編語言
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。