2018年11月26日 练习3

#公鸡5元,母鸡3元,小鸡3只一元,总数money,需要买s只,则三鸡多少只
money=int(input("请输入多少金额:"))
s=int(input('需要多少只'))
for i in range(1,money//5+1):
    for j in range(1,money//3+1):
        for k in range(1,s+1):
            if 5*i+j*3+k/3==money and i+j+k==s:
                print("公鸡:",i,"母鸡",j,"小鸡",k)
else:
    print("end")

 

求列表中任意两元素相加为9的元素集合:

 

a=[]
nums=[2,7,11,15,1,8,7,-2,5,4]
for i in range(len(nums)):
    for j in range (i,len(nums)):
        if nums[i]+nums[j]==9 :
            a.append((nums[i],nums[j]))#注意多加一个括号
print (a)

 

通过切片来分页:用while True循环 不断输入

user_list = [
        {"name":"alex1","email":"alex1@live.com","pwd":"pwd1"}
    ]

for i in range(1,301):
    temp={"name":"alex"+str(i),"email":"alex@live.com"+str(i),"pwd":"pwd"+str(i)}
    user_list.append(temp)

while True:
    s=int(input("请输入页码"))
    x=user_list[10*s-10:10*s]#切片
    for i in x:
        print (i)

31购物车

#创建店铺清单+序号
shop_buy={}#商铺字典
goods    =    [
{"name":"电脑","price":1999},
{"name":"鼠标","price":10},
{"name":"游艇","price":20},
{"name":"美女","price":98},
]
for num,product in enumerate(goods,start=1):
    print ("序号",num,product)
    shop_buy[num]=product #创建带序号的商铺字典

print("我叫分割线".center(100,"*"))

# 输入总资产
while True:
    all_money=input("请输入总资产,如果退出请输入quit:")
    if all_money.isdigit():
        all_money=int(all_money)
        break
    elif all_money=="quit" :
        print("欢迎下次再来")
        exit()
    else:
        print("请重新输入数字")

#创建购物篮

user_buy=[]#用户的购物篮

while True:
    product_list = input("请输入序列号,退出请输入quit,买单请输入buy:")#输入购物列表
    if product_list=="quit":
        break
    elif product_list.isdigit():#判断输入的是否是数字
        if 1<=int(product_list)<=len(shop_buy):#判断输入的序号是否不在选购范围内
            user_buy.append(shop_buy[int(product_list)]["name"])#生成已经选购的购物列表,索引的方法
            #定义购物篮中各种物品数量
            computer_num = user_buy.count("电脑")
            mouse_num = user_buy.count("鼠标")
            sea_num = user_buy.count("游艇")
            beauty_num = user_buy.count("美女")
            cash = computer_num * 1999 + mouse_num * 10 + sea_num * 20 + beauty_num * 98
            if cash<=all_money:
                print("\n>>>购物篮中的总额:".center(10,">"),cash,"余额:".center(10,">"),all_money-cash)
                print("\n购物篮:\n", "电脑", computer_num, "鼠标", mouse_num, "游艇", sea_num, "美女", beauty_num)  # 计算购物篮已添加的东西
            else:
                print("余额不足,不能添加该商品".center(30,"-"))
                user_buy.remove(shop_buy[int(product_list)]["name"])#购物篮中删除无法添加的商品
                computer_num = user_buy.count("电脑")
                mouse_num = user_buy.count("鼠标")
                sea_num = user_buy.count("游艇")
                beauty_num = user_buy.count("美女")
                cash = computer_num * 1999 + mouse_num * 10 + sea_num * 20 + beauty_num * 98
                print("\n>>>购物篮中的总额:".center(10, ">"), cash)
                print("\n购物篮:\n", "电脑", computer_num, "鼠标", mouse_num, "游艇", sea_num, "美女", beauty_num)  # 计算购物篮已添加的东西
        else:
            print("请输入正确的商品序号")
    elif product_list=="buy":#判断是否买单
        if len(user_buy)==0:#判断是否购物车为空
            print("未添加商品,请添加商品")
        else:
            print("消费总额:",cash,"\n最终买单蓝:\n", "电脑", computer_num, "鼠标", mouse_num, "游艇", sea_num, "美女", beauty_num)
            print("欢迎下次光临")
            break

    else:
        print("请输入序号(数字)")

 

posted @ 2018-11-26 21:21  小圣庄  阅读(179)  评论(0编辑  收藏  举报