首页 > 代码库 > python循环之for循环与模拟购物车小程序
python循环之for循环与模拟购物车小程序
for循环
for 变量 in 范围: 代码块... contune #跳出本次循环接着执行下一次循环 for 变量 in 范围: 代码块... break #跳出本层循环,回到上一个for循环 else: #其实for循环和while循环都有else子句,不过是当循环完全执行了才会执行 代码块... 其他主程序代码块...
#作业1--跳出三层for循环
for i in range(5): print("i---",i) for j in range(5): print("j---",j) for k in range(5): if k == 2: print("k---",k) break else: continue break else: continue ‘‘‘ 作业2-购物车程序 顾客可以根据自己的工资购买想要的东西,每次购买完成会计算剩余的金额 ‘‘‘
print("----------Welcome to oldboy market----------") print("------Today You can buy all of these:-------") print("--Iphone<>Computer<>Coffee<>Book<>Clothes--") shopping_car = [] client_salary = int(input("Please input your salary:")) while True: items = [[‘Iphone‘, 5000], [‘Computer‘, 7999], [‘Coffee‘, 30], [‘Book‘, 80], [‘Clother‘, 600]] i = 1 enable_list = [] # 用户能购买的东西 disable_list = [] # 用户不能买的东西 cost_list = [] # 能购买的商品价格 name_list = [] # 能购买的商品名字 print("You can buy these:\n") for item in items: if item[1] <= client_salary: enable_list.append(item) cost_list.append(item[1]) name_list.append(item[0]) print(i, item[0], " ", item[1]) i += 1 else: disable_list.append(item) continue print("Q quit shopping") print("------------------------------------------------------") print("You can not buy these,because you don‘t have enough money!\n", disable_list) print("------------------------------------------------------") choice = input("what‘s your choice number or quit:") if choice == ‘q‘ or choice == ‘Q‘: print("Today you have buy these :",shopping_car) break else: choice = int(choice)-1 if choice <= len(enable_list): if cost_list[choice] <= client_salary: shopping_car.append(enable_list[choice]) #将选择的商品加到购物车 client_salary = client_salary - cost_list[choice] print("The " ,enable_list[choice] , " is added to the shoppingcart \nyour balance is ",client_salary) print("------------------------------------------------------") print("Have Buy:",shopping_car) print("------------------------------------------------------") else: print("You don‘t have enough money to buy this!") continue else: print("Your choice is wrong,Please try again!") continue
python循环之for循环与模拟购物车小程序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。