运算符
product_list = [['phone', 5000], ['book1', 100], ['变形金刚模型', 500], ['小鱼仔一箱', 50], ['伊利牛奶', 100] ] shopping_cart = {}#字义购物车为一个字典 salary = int(input("input you salary: ")) while True: index = 0 for product in product_list: print(index,product) index +=1 choice = input("请输入要购买的商品编号>>>:").strip() if choice.isdigit(): choice = int(choice) if choice >= 0 and choice < len(product_list): product = product_list[choice] if product[1] <= salary: if product[0] in shopping_cart:#判断是否在字典中的方法 shopping_cart[product[0]] [1] +=1 else: shopping_cart[product[0]] = [product[1],1] salary -= product[1] print("添加商品" + product[0] +"到购物车", "你现在的余额为" + str(salary)) else: print("您的余额不足,产品的价格是" + str(product[1]) + "你还差" + str(product[1] - salary)) else: print("商品不存在") elif choice == "q": print("---------已购买商品列表-------") id_counter = 1 total_cost = 0
print("id 商品 数量 单价 总价") for key in shopping_cart: print("%10s%10s%10s%10s%10s" %(id_counter, key, shopping_cart[key][1], shopping_cart[key][0], shopping_cart[key][1]*shopping_cart[key][0]))#格式化输出 id_counter +=1 total_cost += shopping_cart[key][1]*shopping_cart[key][0] print("总花费%s" % (total_cost)) break else: print("无此选项")
#######################################################################
if []: print"yes"
空列表,空字典,空元组,空集合,空字符串,0,布尔值都为假,
算数运算:
比较运算:
赋值运算:
逻辑运算:
成员运算:
身份运算:
位运算:
menu = { '北京':{ '昌平':{ '沙河':{ '老男孩':100000 } } }, '河南':{} } exit_flag = False while not exit_flag:#程序开始打印一级菜单 for key in menu: print(key) choice = input(">:").strip()#第1次用户选择 if len(choice) == 0:continue if choice == 'b': break if choice == 'q': exit_flag = True continue#直接退出不再打印下边的内容 if choice in menu:#省级菜单存在,进入下一级 while not exit_flag: next_layer = menu[choice] for key2 in next_layer: print(key2) choice2 = input(">>:").strip() # 第2次用户选择 if len(choice2) == 0: continue if choice2 == 'b': break if choice2 == 'q': exit_flag = True continue if choice2 in next_layer:#市级菜单存在,进入下一级 while not exit_flag: next_layer2 = next_layer[choice2] for key3 in next_layer2: print(key3) choice3 = input(">>>:").strip() # 第3次用户选择 if len(choice3) == 0: continue if choice3 == 'b': break if choice3 == 'q': exit_flag = True continue if choice3 in next_layer2: # 县级菜单存在,进入下一级 while not exit_flag: next_layer3 = next_layer2[choice3] for key4 in next_layer3: print(key4) choice4 = input(">>>>:").strip() # 第4次用户选择 if choice4 == 'b':break if choice4 == 'q': exit_flag =True continue