首页 > 代码库 > Python基础-小程序练习
Python基础-小程序练习
一、 从第3层循环直接跳出所有循环
break_flag = False count = 0 while break_flag == False: print("-第一层") while break_flag == False: print("第二层") while break_flag == False: count += 1 if count > 10: break_flag = True print("第三层") print("keep going...")
# break_flag = False count = 0 while count < 3: print("-第一层") while count < 3: print("第二层") while count <= 10: count += 1 # if count > 10: # break_flag = True print("第三层") print("keep going...")
二、 购物车程序
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 while True: index = 0 for goods in goods_list: #打印商品列表 print(index,goods) index += 1 choice = 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 += 1 print(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) break else: print(‘Have no this goods‘)
Python基础-小程序练习
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。