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
时间: 2024-08-08 22:08:27

python循环之for循环与模拟购物车小程序的相关文章

Python入门基础---购物车小程序

1.购物车小程序: 1.1用户输入工资取60% 1.2打印输出商品菜单 1.3由用户输入数字选择 #__author:Mifen #date: 2018/11/27 # 购物车程序 #把工资作为账户的余额 salary = int (input('你的工资为:')) funds = salary * 0.6 # 取工资的60% #自定义本地商品数据列表[商品名称,价格,库存] menu = [['保留使用,不存数据'],['iPhone7',6000,30],['Notebook',9000,3

python路5__购物车小程序练习

1,购物车小程序 需求: (1),启动程序后,打印全部商品列表,用户输入工资 (2),许用户根据商品编号购买商品 (3),用户购买商品,余额不足就退出程序,并打印信息 (4),可随时退出,退出时,打印已购买商品和余额 2,流程图 3,代码 #!/usr/bin/python3 Product_list = [ ('Doido钻戒 ',8000), ('ROLEX手表',20000), ('HuaWei P10',4000), ('AppleWatch',2000), ('Ipad',1000),

简单的购物车小程序

1 # -*- coding:utf-8 -*- #简单的购物车小程序 author:李学松 2 shopping_cart =[] 3 product_list_tatol = "---product list----" 4 welcome = "-----------welcome to shopping marketi----------" 5 product_list = [ 6 ('iphone',5800), 7 ('lenovo',900), 8 ('

python模拟购物车小项目

# 模拟购物车:# 要求:# 1,用户先给自己的账户充钱:比如先充3000元.# 2,有如下的一个格式:# goods = [{"name": "电脑", "price": 1999},# {"name": "鼠标", "price": 10},# {"name": "游艇", "price": 20},# {"nam

Python实现购物车小程序

开发环境,win7.Python3.6.Pycharm社区版2017 作业需求: 购物车程序:1.启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表    #再次登陆,输入工资进行充值服务,有没有人性化2.允许用户根据商品编号购买商品 3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒4.可随时退出,退出时,打印已购买商品和余额5.在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示6.用户下一次登录后,输入用户名密码,直接回到上次的状

python基础练习之购物车小程序

此小程序购物流程为: 启动时欢迎语,提示输入购物预算金额,打印购物菜单,选择商品,商品可重复选择购买,当购物预算金额小于购买商品的金额时,返回购物列表并提示预算金额不足. #!/usr/bin/python # -*- coding:UTF-8 -*- shangpin = {'iphone7':6000,'mate9':5000,'macbook':10000,'note5':3000} yusuan = 30000 gouwu_list = [] print '==欢迎来到大猫商城==' p

购物车小程序(while循环,列表)

1 while True: 2 salary = input("please input your salary:") 3 if salary.isdigit(): 4 salary=int (salary) 5 break 6 else: 7 print("Invalid input! please input a number:") 8 9 product_cart = [['iphone6s',5800], #定义商品列表 10 ['macbook',9000

python学习笔记——贰之购物车小程序_客户端

客户端大概流程 :买家系统 首先输入买家他的余额,之后会进行,通过文件里的来余额判断他是否上次消费过.如果有信息,就按照文件里的余额计算,替换掉买家输入的余额,如果没有信息证明客户是第一次来,就用买家输入的余额. 然后循环输出商品下标,和商品信息.客户通过下标来选择商品.选择的商品会被加入到 购物车列表 中,然后通过下标[1]得到商品的金额.计算出消费金额,这个消费金额 如果大于余额 那么 从购物车列表里删除这个商品 告知 余额不足,并提示买多少钱以内的商品.每加进一个商品,都会计算购物车的总金

python学习-购物车小程序

购物车功能要求: 要求用户输入总资产,例如:2000显示商品列表,让用户根据序号选择商品,加入购物车购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功.附加:可充值.某商品移除购物车 1 goods = [ 2 {"name": "电脑", "price": 1999}, 3 {"name": "鼠标", "price": 10}, 4 {"name":