Day10 【小程序】商城管理(购物车小程序的增强版)

功能:(两个分开的程序)

用户入口:

  1. 商品信息存在文件里;
  2. 已购商品、余额记录。

商家入口:可以添加商品,修改商品价格;

 

商家入口:

# Author:q1.ang

data={}
data_save=''

product=''
price=''

cache_product=''
cache_price=''

new_product=''
new_price=''

confirm=''
confirm_save=''
confirm_replace=''
confirm_continue='n'

f=open('shop_data.txt')
shop_data=f.readlines()
f.close()

print(shop_data)


while confirm_continue=='n':

    for i in shop_data:  # 打印商品和价格
        i2 = i.rstrip()
        product = i2.split(',')[0]
        price = i2.split(',')[1]

        data[product] = price

        print(shop_data.index(i) + 1, '.', product, '    ¥:', price)

    operate = input('Please choose [edit] [add] or [delete] product data:')

    if operate=='edit':
            edit_num=input('Please input the product number:')  #选择商品
            while not edit_num.isdigit():
                edit_num=input('input error,please input again:')
            edit_num_int=int(edit_num)

            product=shop_data[edit_num_int-1].split(',')[0] #获取str型商品和价格
            price=shop_data[edit_num_int-1].split(',')[1]

            if product in data: #如果商品存在

                cache_product=product #缓存
                cache_price=data[product]

                print(product,'    ¥:',data[product])

                edit_data=input('Please input the new product [name] or [price]:')

                if edit_data.isdigit():#如果修改的是价格
                    new_product=product
                    new_price=edit_data

                else:      #如果修改的是商品名称
                    new_product = edit_data
                    new_price = data[product]

                print(new_product, '', new_price)

                confirm = input('Please confirm the edit to save(y/n):')
                while confirm != 'y' and confirm!='n':
                    confirm=input('Input error:please input again(y/n):')

                if confirm=='y':
                    data.pop(product)
                    data[new_product]=new_price
                    print('Saved Successful !')
                elif confirm=='n':
                    pass

    elif operate=='add':
        product=input('Please input the product name:') #输入商品名称
        while product.isdigit():
            product_digit_confirm=input('Are you sure this is product name?(y/n)')
            while product_digit_confirm!='y' and product_digit_confirm!='n':
                product_digit_confirm = input('Input error:please input again(y/n):')
            if product_digit_confirm=='y':
                break
            else:
                product = input('Please input the product name again:')

        price=input('Please input the product price:') #输入商品价格
        while not price.isdigit():
            price_digit_confirm=input('Are you sure this is product price?(y/n)')
            while price_digit_confirm!='y' and price_digit_confirm!='n':
                price_digit_confirm = input('Input error:please input again(y/n):')
            if price_digit_confirm=='y':
                break
            else:
                price = input('Please input the product price again:')

        print(product, '',price)

        if product in data:
            confirm_replace=input('The data had this product,are you decided to replace it(y/n):')
            while confirm_replace!='y' and confirm_replace!='n':
                confirm_replace = input('Input error:please input again(y/n):')
        else:
            confirm_save=input('Are you dicided to save it(y/n):')
            while confirm_save!='y' and confirm_save!='n':
                confirm_save = input('Input error:please input again(y/n):')

        if confirm_replace == 'n'or confirm_save=='n':
            pass
        else:
            data[product]=price
    elif operate == 'delete':
        delete_produce=input('Delete product number:')
        while not delete_produce.isdigit():
            delete_produce = input('Input error,write the delete product number again:')
        delete_produce_int=int(delete_produce)
        cache_product=shop_data[delete_produce_int - 1].split(',')[0]
        print(cache_product,'',data[cache_product])

        confirm_delete=input('Are you dicided to delete it(y/n):')
        while confirm_delete!='y'and confirm_delete!='n':
            confirm_delete = input('Input error:please input again(y/n):')

        if confirm_delete=='y':
            data.pop(cache_product)
        else:
            pass

    confirm_continue=input('Are you exit?(y/n)')

print(data)
for i in data:
    data_save+=i+','+data[i]+'\n'

f=open('shop_data.txt','w')
f.write(data_save)
f.close()

 



 

 

posted @ 2018-04-17 22:26  q1ang  阅读(156)  评论(0编辑  收藏  举报