购物车小程序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了购物车小程序相关的知识,希望对你有一定的参考价值。
# __author__:Nathaniel # 2017/6/12 # 登录 user_name = ‘name‘ password = ‘password‘ login_flag = False # 成功登录则为True for i in range(3): input_name = input(‘please input the account num:‘) input_password = input(‘please input the password:‘) # 账户校验 if user_name == input_name and password == input_password: print(‘welcome!%s.‘ % user_name) login_flag = True break else: print(‘invalid account or password,you still have %d chances to try.‘ % (2-i)) else: print(‘where is your face?‘) # 登陆成功 login_flag为True,进入商城 if login_flag: # 存放商品及其价格信息 commodity_list = [[‘computer‘, 6300], [‘bicycle‘, 300], [‘book‘, 45], [‘bag‘, 100], [‘watch‘, 500], [‘flower‘, 999]] # 账户余额 balance = int(input(‘please enter your budget:‘)) # 存放商品的购物车 shopping_cart = [] # 打印商品列表 for commodity in commodity_list: print(commodity_list.index(commodity) + 1, ‘. ‘, commodity[0], ‘:‘, commodity[1]) # 开始购物 quit_flag = True # 选择购买的商品编号 commodity_num = int(input(‘Please enter the commodity number that you want to purchase:‘)) while quit_flag: # 余额充足,付款 if (balance - commodity_list[commodity_num - 1][1]) >= 0: print(‘%s is already added your shopping cart‘ % commodity_list[commodity_num - 1][0]) balance = balance - commodity_list[commodity_num - 1][1] shopping_cart.append(commodity_list[commodity_num - 1][0]) # 继续购买或者结算 is_quit = input(‘Enter the commodity number to continue to purchase, press enter key to pay.‘) if is_quit == ‘‘: quit_flag = False else: commodity_num = int(is_quit) # 余额不足 else: # 继续购买或者结算 is_quit = input(‘your balance is not enough.‘ ‘Enter the commodity number to continue to purchase, press enter key to pay.‘) if is_quit == ‘‘: quit_flag = False else: commodity_num = int(is_quit) # 结算 print(‘You have bought:‘, end=‘‘) for commodity_in_shopping_cart in shopping_cart: print(commodity_in_shopping_cart, end=‘;‘) print(‘\n your balance is %d‘ % balance)
改天新增一个2.0版本 , 增加非法输入的校验
程序小白
以上是关于购物车小程序的主要内容,如果未能解决你的问题,请参考以下文章