python新手自学教程2

版权声明:若你喜欢 请随意转载 但请标明出处和作者 https://blog.csdn.net/geesegeese/article/details/82633362

习题:编写商店系统,商品,购物,选择

思路:用列表加各种判断和循环

shangping_list=[(‘iphone’,7000),(‘samaung’,6000),(‘huawei’,5000),(‘xiaomi’,4000)] #创建商品列表

shopping_list=[] #创建购买空列表
salary=input(“input in slary:”) #用户输入
if salary.isdigit(): #判断为10进制
salary=int(salary) #判断为整数
while True: #当为真
for index,item in enumerate(shangping_list): #打印索引和项目
print(index,item)
user_choice= input(“you are choice:”) #用户选择
if user_choice.isdigit(): #10进制
user_choice=int(user_choice) #整数
if user_choice< len(shangping_list) and user_choice>=0: #限制选择必须在列表中
p_item=shangping_list[user_choice] #
if p_item[1] <= salary: #判断用户输入对比买的起
shopping_list.append(p_item) #购买信息加入购物列表
salary -= p_item[1] # 把剩余金额重新赋予用户输入
print(“Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m” %(p_item,salary) ) #\033为各种高亮颜色
else:
print(“\033[41;1m你的余额只剩[%s]啦,还买个毛线\033[0m” % salary)
else:
print(“product code [%s] is not exist!”% user_choice)
elif user_choice == ‘q’: #退出
print(“——–shopping list——”)
for p in shopping_list: #打印购物列表
print(p)
print(“Your current balance:”,salary)
exit()
else:
print(“invalid option”)

猜你喜欢

转载自blog.csdn.net/geesegeese/article/details/82633362