前言:初學者對python的流程語句有一定的了解,但是運用起來總會磕磕碰碰。本文總結了一些初學者在學習python時做的經典案例
一、名片管理系統(限單個名片)

info = {'name':'jam', 'age': 24, 'high':171 } while True: print('='*60) #字符串拼接可以用* print('===名片管理系統===') print('1.改 2.刪 3.查 4.增 5.退出') c=input('請選擇:') if c == '1': res = input('請輸入要修改的關鍵詞:') if res == 'name': info['name']=input('請輸入對應內容:') print(info) elif res == 'age': info['age']=input('請輸入對應內容:') print(info) elif res =='high': info['high']=input('請輸入對應內容:') print(info) if c == '2': res1 =input('請輸入要刪除的關鍵詞:') print(info.pop(res1)) print(info) if c == '3': res2=input('請輸入要查看的關鍵詞:') print(info.get(res2)) if c == '4': res3=input('請輸入要增加的內容:') v1=input('請輸入value:') print(info.update({}.fromkeys((res3,),v1))) print(info) if c == '5': print('退出本次操作') else: print('請輸入12345')
二、名片管理系統

l1=[] #使用列表,利用下標進行增刪改查 while True: print('*'*60) print('==============歡迎進入名片管理系統==============') print('1.查看名片') print('2.添加名片') print('3.修改名片') print('4.刪除名片') print('5.退出系統') choose=input('請選擇:') #input輸出都是字符串 if choose=='1': i=0 while i < len(l1): print('%s->姓名:%s|年齡:%s|身高:%s' % (i,l1[i]['name'],l1[i]['age'],l1[i]['high'])) #第一個%s輸出行號,從0開始 i+=1 else: print('空') elif choose == '2': name=input('name:').strip() #input().strip去除空格 age=input('age:').strip() high=input('high:').strip() info={'name':name, 'age':age, 'high':high } l1.append(info) print('添加成功') elif choose == '3': revise=input('請選擇要修改的名片:') name1=input('name:').strip() age1=input('age:').strip() highl=input('high:').strip() if name1: #加入條件語句是為了讓用戶知道有哪些關鍵詞 l1[int(revise)]['name']=name1 if age1: l1[int(revise)]['age']=age1 if highl: l1[int(revise)]['high']=highl print('修改成功') elif choose == '4': de1=input('請輸入要刪除的名片:') l1.remove(l1[int(de1)]) print('刪除成功') elif choose == '5': print('退出系統') break else: print('輸入錯誤,請重新輸入')
三、猜拳小游戲

import random #import代表導入模塊,random是隨機數模塊 lost = 0 win = 0 ping = 0 while True: #循環 print('='*60) print('****************歡迎來猜拳*****************') print('贏:%s 平:%s 輸:%s' % (win,ping,lost)) print('1.石頭 2.剪刀 3.布 4.退出') robot = random.choice( ['剪刀','石頭','布']) h = input('請出:') if (h == '1' and robot == '剪刀')or (h == '2' and robot == '布') or (h== '3' and robot == '石頭'): #贏 win+=1 print('you are the winner') elif (h == '1' and robot == '石頭')or (h == '2' and robot == '剪刀') or (h== '3' and robot=='布'): #平 ping+=1 print('ping') elif (h == '1' and robot == '布') or (h == '2' and robot == '石頭') or (h == '3' and robot=='剪刀'): #輸 lost+=1 print('you are loser') elif h == '4': print('退出系統') break #退出循環 else: print('輸入錯誤,請重新輸入')
四、用戶登錄界面(單個),輸入三次錯誤密碼,退出程序

user = 'root' paw = 'root' count = 0 print('***********登錄系統***********') while True: user1 = input('username:') passwd1 = input('passwd:') if user1 and passwd1: print('登錄成功') break else: count+=1 print('用戶名或密碼錯誤,請重新輸入', count) if count == 3: print('密碼輸入超過三次,請求失敗')
五、用戶登錄界面(多個),支持多個用戶登錄,用戶3次認證失敗后,退出程序,再次啟動程序登錄時,還是鎖定狀態

print('*****************登錄系統*****************') info = {'name1':'0330', 'name2':'1234', 'name3':'806' } #多個用戶及其對應密碼 count=0 print('1.登錄 2.注冊') while True: choose = input('請選擇登錄or注冊:') if choose=='1': user = input('用戶名:') with open(r'C:\Users\jam\Desktop\b.txt', 'r', encoding='utf-8') as f: data = f.read() if user in data: print('滾蛋吧') break psw = input('密碼:') if info[user]== psw: print('登錄成功') #用戶和密碼一一對應,登陸成功 else: count += 1 print('密碼錯誤') if count == 3: #輸入三次錯誤密碼。退出 with open(r'C:\Users\jam\Desktop\b.txt','w',encoding='utf-8') as f: f.write(user) print('你的賬號被封了') break elif choose == '2': #創建新用戶 user1=input('請輸入用戶名:') psw1=input('請輸入密碼:') info.setdefault(user1,psw1) print(info) break
六、乘車安檢

has_ticket=input('====>:').strip() knife_length=int(input('====>').strip()) if has_ticket: print('有票,請進行安檢') if knife_length >=20: print('安檢通過,請上車') else: print('滾粗') else: print('請先購票')
在編寫程序時,一定要先理清思路,知道自己要做什么,怎么做,分析好了再下手。