python复习购物车程序

时间:2022-11-24 18:35:44

个人学习总结:

无他,唯手熟尔!多敲多练才是王道

python 第三课 元组的灵活运用&字符串的诸多操作

Program01

'''
时间 2018年2月12日12:15:28
目的 购物车程序

内容 1. 启动程序后让用户输入工资然后打印商品列表

2. 允许用户根据商品编号购买商品

3. 用户选择商品后,检测余额是否充足,够则直接扣款,不够提醒

4. 可随时退出,退出时,打印已经购买的商品和余额

# 假定用户不会输错

'''
shop_list = [["牙膏",10],["牙刷",5],["洗面奶",30],["肥皂",5],["毛巾",5],["洗脸盆",15],["牙缸",10]]

shop_car = []

customer_salary = 0

money_left = 0

clear_num = 0

exit_shop = False

# print(shop_list.index(["牙缸",10])) # 用于查看牙缸的位置

customer_salary = int(input("please input your salary:"))

while True:
# 展示购物列表
for i in range(0,len(shop_list)):
print(i,"\b",shop_list[i][0],shop_list[i][1])

# 选择购买的商品
num = int(input("choose the goods>>>"))
shop_car.append(shop_list[num])

# 显示已经购买的商品
print("All the goods in car",shop_car)

while not exit_shop:

# 计算商品总额
sum_goods = 0
for i in shop_car[:]:
sum_goods += i[1]
print("the sum of the goods:",sum_goods)

# 检测余额是否充足,够则直接扣款,不够提醒,并支持清除指定商品

if sum_goods <= customer_salary:
money_left = customer_salary - sum_goods
print("money_left:",money_left)
exit_shop = True
else:
print("the salary is not enough!")
# 打印出已经购买的商品
for j in range(0,len(shop_car)):
print(j, shop_car[j][0], shop_car[j][1])
# 支持清空购物车指定位置商品
clear_num = int(input("drop the goods:"))
shop_car.pop(clear_num)
print(shop_car)

exit_shop = False # 退出购买的判断

# 用户自主退出 打印商品以余额
choice = input("end press q>>>")
if choice == 'q':
print("------shopping list------")
for index,item in enumerate(shop_car): # 将元组内的元素以下标和元素内容形式打印
print(index,item)
print("money left:",money_left)
break # exit()

字符串的诸多操作

MYK
2018年2月12日