Python基础-小程序练习
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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基础-小程序练习的主要内容,如果未能解决你的问题,请参考以下文章
Python基础教程3——教你用Python做个简单的加密程序(还基础什么呀,直接来练习吧,带源码)