标签:book price ota break log isp span open int
#作业一: 跳出多层循环,三层循环,在最里层,直接跳出
exit_flag = True #退出的标志位 for i in range(3): if exit_flag == True: print(‘i---->‘,i) for j in range(3): if exit_flag == True: print(‘j--->‘,j) for k in range(3): print(‘k--->‘,k) if k==1: exit_flag = False break else: break else: break print(‘last line!‘)
# 作业二: 购物车程序:
#你可以买下的东西:1 iPhone:5800 2 coffee 30 3 book 50 4 condom 90
#选择你要买的东西
#判断是否买的起
#放入购物车,扣钱,打印余额
#还可以继续选择购买
money = int(input("请输入你有多少元:")) goods = [‘iphone‘, ‘coffee‘,‘book‘,‘shirt‘,‘chocolate‘] #商品列表 price = [ 5800,300,500,1000,50] #商品单价 buy_num = [0,0,0,0,0] #加入购物车每一种商品的个数 total = 0 #加入购物车一共有几件,作为是否有够买商品的判断 exit_flag = True #退出标志位 remain_money = money #剩余的钱 print("我们商店有以下的商品:") for i in range(goods.__len__()): #打印商品列表 print(i,goods[i],price[i]) while exit_flag == True: number = int(input("请输入你想买的商品标号:")) if number >= goods.__len__(): choice = input("商品不存在,退出?(y,n)") if choice == "y": exit_flag = False else: count = int(input("请输入你想买几件:")) buy_num[number] +=count #在buy_num中到对应位置存下购买商品个数 remain_money = remain_money - price[number]*count #扣钱 if remain_money >= 0: print("你还有",remain_money,"元") choice = input("退出?(y,n)") if choice == "y": exit_flag = False else: print("钱不够了!!!") remain_money = remain_money + price[number]*count #没有购买成功,操作前的钱和购买数量 buy_num[number] -=count #没有购买成功,操作前的钱和购买数量 print("你还有",remain_money,"元","你选择的商品需要花费",price[number]*count,"元") choice = input("退出?(y,n)") if choice == "y": exit_flag = False for i in range(buy_num.__len__()): total += buy_num[i] if total == 0: print("你没有购买任何商品") else: print("------------------------------购物清单---------------------------------") print("你购买了以下商品:") for i in range(buy_num.__len__()): if buy_num[i] > 0: print(goods[i],‘----‘,buy_num[i],‘个‘) print(‘你还有‘,remain_money,‘元‘)
标签:book price ota break log isp span open int
原文地址:http://www.cnblogs.com/yangming111/p/6637546.html