日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python基础-小程序练习(跳出多层循环,购物车,多级菜单,用户登录)

發布時間:2023/12/18 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python基础-小程序练习(跳出多层循环,购物车,多级菜单,用户登录) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、 從第3層循環直接跳出所有循環

break_flag = False count = 0 while break_flag == False:print("-第一層")while break_flag == False:print("第二層")while break_flag == False:count += 1if count > 10:break_flag = Trueprint("第三層") print("keep going...") with break_flag

?

?

# break_flag = False count = 0 while count < 3:print("-第一層")while count < 3:print("第二層")while count <= 10:count += 1# if count > 10:# break_flag = Trueprint("第三層") print("keep going...") without break_flag

?

?

二、 購物車程序

goods_list = [['Iphone7',5800],['Coffee',30],['Book',99],['Bike',199],['Vivi X9',2499]] #商品列表 shopping_cart = [] #用戶購物車列表 salary = int(input('input your salary:')) #用戶薪水 m = salary k = 0while True:index = 0for goods in goods_list: #打印商品列表print(index,goods)index += 1choice = input('>>>:').strip()if choice.isdigit():choice = int(choice)if choice >= 0 and choice < len(goods_list):goods = goods_list[choice]if goods[1] <= salary: #判斷用戶是否帶了足夠的錢來支付所選商品 shopping_cart.append(goods)salary -= goods[1]print('Add goods '+str(goods[0])+' into shopping cart! Your current balance:'+str(salary))else:print('No enough money!The price is:'+str(goods[1])+'! Need more:'+str(goods[1]-salary))else:print('Have no this goods!')elif choice == 'q':print('----------Your shopping cart----------')print('ID goods quantity price total')for i in range(len(goods_list)):j = shopping_cart.count(goods_list[i])if j > 0 :k += 1print(k,'\t',goods_list[i][0],'\t',j,'\t\t',goods_list[i][1],'\t',j * goods_list[i][1])print('Total price is:',m - salary)breakelse:print('Have no this goods') 購物車程序

goods_list = [['Iphone7',5800],['Coffee',30],['Book',99],['Bike',199],['Vivi X9',2499]] # 商品列表 shopping_cart = {} # 用戶購物車列表從列表變為字典 salary = int(input('input your salary:')) # 用戶薪水 # m = salary #不需要此項 # k = 0 #用戶購物車商品打印ID,移動位置至打印循環外while True:index = 0for goods in goods_list: # 打印商品列表print(index, goods)index += 1choice = input('>>>:').strip()if choice.isdigit(): # 判斷是否為數字choice = int(choice)if choice >= 0 and choice < len(goods_list): # 商品存在goods = goods_list[choice]if goods[1] <= salary: # 判斷用戶是否帶了足夠的錢來支付所選商品if goods[0] in shopping_cart: # 之前買過shopping_cart[goods[0]][1] += 1 # 購物數量加1else:shopping_cart[goods[0]] = [goods[1], 1] # 創建一條新增商品的購買記錄salary -= goods[1] # 扣錢print('Add goods ' + str(goods[0]) + ' into shopping cart! Your current balance:' + str(salary))else:print('No enough money!The price is:' + str(goods[1]) + '! Need more:' + str(goods[1] - salary))else:print('Have no this goods!')elif choice == 'q':id_counter = 1 # 初始化商品IDtotal_cost = 0 # 初始化商品總花費print('----------Your shopping cart----------')print('ID goods quantity price total')for i in shopping_cart:print("%-5s%-10s%-12s%-10s%-s"% (id_counter, i, shopping_cart[i][1], shopping_cart[i][0],shopping_cart[i][0] * shopping_cart[i][1]))id_counter += 1 # 商品ID自加1total_cost += shopping_cart[i][0] * shopping_cart[i][1] # 用戶已購商品總花費print('Total price is:', total_cost)breakelse:print('Have no this goods') 購物車程序優化,使用字典

三、多級菜單

要求:?

  • 打印省、市、縣三級菜單
  • 可返回上一級(b,返回)
  • 可隨時退出程序(q,退出)
  • menu = {'北京':{'海淀':{'五道口':{'soho':{},'網易':{},'google':{}},'中關村':{'愛奇藝':{},'汽車之家':{},'youku':{},},'上地':{'百度':{},},},'昌平':{'沙河':{'老男孩':{},'北航':{},},'天通苑':{},'回龍觀':{},},'朝陽':{},'東城':{},},'上海':{'閔行':{"人民廣場":{'炸雞店':{}}},'閘北':{'火車戰':{'攜程':{}}},'浦東':{},},'山東':{}, }exit_flag = False current_layer = menulayers = [menu]while not exit_flag:for k in current_layer:print(k)choice = input(">>:").strip()if choice == "b":current_layer = layers[-1]#print("change to laster", current_layer) layers.pop()elif choice == 'q':breakelif choice not in current_layer:continueelse:layers.append(current_layer)current_layer = current_layer[choice] View Code

    ?

    四、用戶登陸程序

    需求:

  • 最多允許用戶嘗試登陸3次
  • 當同一用戶名3次密碼均不正確時,鎖定該用戶
  • 1 with open('account',encoding='utf8') as f_account, open('lockedlist', 'a+') as f_locked: 2 l = [] #定義用戶名驗證列表,存放黑名單數據 3 f_locked.seek(0) #"a+"模式打開后,文件位置位于末尾,要遍歷文件內容,需要將指針移至文件起始位置 4 for locked_info in f_locked.readlines(): #遍歷黑名單 5 l.append(locked_info.strip()) #將黑名單數據添加進列表,注意:需要將文件中的換行符脫掉 6 7 c = [] #定義用戶登錄名列表,存儲用戶嘗試的登錄名 8 count = 1 #登陸次數計數器 9 flag = True #登陸循環控制開關 10 while flag and count < 4: 11 user = input('Please input username:') #輸入用戶名 12 pwd = input('Please input password:') #輸入用戶密碼 13 if user in l: #用戶名在黑名單中 14 print("This user is in blacklist,can't log in!") #打印提示信息 15 continue 16 c.append(user) 17 for info in f_account: #用戶名不在黑名單中,遍歷用戶登陸文件 18 user_name, user_pwd = info.strip().split(',') #將文件中的用戶名和登陸密碼賦值給判定變量 19 if user == user_name: #用戶名符合 20 if pwd == user_pwd: #對應密碼符合 21 print('Welcome %s' % user) #打印登陸成功信息 22 flag = False #登陸成功,跳出登陸程序 23 break 24 count += 1 #對應密碼不符合,while循環計數器加1 25 break 26 count += 1 #用戶名不符合,while循環計數器加1 27 break 28 if count == 4: #如果不同用戶名密碼輸錯3次,關閉程序 29 print('More than 3 times wrong!') #打印提示信息 30 if len(c) == 3 and c[0] == c[1] == c[2]: #如果相同用戶名密碼輸錯3次,將該用戶名加入黑名單,限制登陸 31 print('This user has been locked!') 32 f_locked.write(user+'\n') #將錯誤3次的用戶名寫入黑名單,注意,加換行符 用戶登陸程序

    ?

    ?

    轉載于:https://www.cnblogs.com/OldJack/p/6642196.html

    創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

    總結

    以上是生活随笔為你收集整理的Python基础-小程序练习(跳出多层循环,购物车,多级菜单,用户登录)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。