黑帽联盟

 找回密码
 会员注册
查看: 1582|回复: 2
打印 上一主题 下一主题

[其它] python语言编写的购物系统-定位原创

[复制链接]

293

主题

18

听众

955

积分

管理员

Rank: 9Rank: 9Rank: 9

  • TA的每日心情
    奋斗
    2023-10-26 13:13
  • 签到天数: 358 天

    [LV.8]以坛为家I

    购物系统功能要求:

    1、前提必须指定用户登录进去,才可进行购买!

    2、登录进去之后,提示用户进行充值金额,否则无法进行购买物品!

    3、充值成功后,提示用户是否进行购买(不购买直接退出系统)

    4、用户同意购买之后,把所有产品列出供用户选择

    5、针对之前用户充值的金额来判断用户哪些用户不能买,并给予提示信息;能购买的话,把物品加入购物框

    6、如果用户输入的产品不存在,也给予相应的提示信息


    实现功能代码:

    #!/usr/bin/python

    objects = ['coffee','car','mobile','Internet','clothes']
    prices = [35,30000,3000,6000,500]
    shop_list = []



    while True:
            name = raw_input("please input your name:")

            if name == 'dingwei':
                    password = raw_input("please input your password:")
                    while password != 'cbh':
                            password = raw_input("please input your password:")
                    print "----------------------------------"
                    print "welcome to login to system!"
                    print "------------------------------------"
            else:
                    print "user %s not found,try again!" % name
                    continue

            self_sal = int(raw_input("please store your money:"))

            while True:
                    if self_sal > 0:
                            print '--------------------------------------------'
                            print 'you had stored %s successfully!' % self_sal
                            break
                    elif self_sal == 0:
                            self_sal = int(raw_input("you store your money failure ,please store money again:"))
                            continue
                    else:
                            self_sal = int(raw_input("you store your money failure ,please store money again:"))
                            continue

            while True:
                    choose = ''
                    purchase = raw_input('Do you want to shop(yes/no):%s' % choose)

                    if purchase == 'yes':
                            for i in objects:
                                    print i,'        ',prices[objects.index(i)]
                    elif purchase == 'no':
                            print '------------------------------------'
                            print 'you had exited system successfully!'
                            break
                    else:
                            continue

                    choice = raw_input('Please input one item to buy:')
                    F_choice = choice.strip()


                    while True:
                            if F_choice in objects:
                                    objects_index = objects.index(F_choice)
                                    objects_price = prices[objects_index]
                                    print F_choice,objects_price
                                    if self_sal > objects_price:
                                            shop_list.append(F_choice)
                                            print 'Added %s into shop list' % F_choice
                                            self_sal = self_sal - objects_price
                                            print "Salary left:",self_sal
                                            break
                                    else:
                                            if self_sal < min(prices):
                                                    print 'Sorry,rest of your salary cannot buy anything!88'
                                                    print "you hava bought these things: %s" % shop_list
                                                    break
                                            else:
                                                    print "Sorry,you cannot afford this product,please try other ones!"
                                                    print "-----------------------------------------------------------------"
                                                    choice = raw_input('please input one item to buy:')
                                                    F_choice = choice.strip()
                                                    continue
                            else:
                                    print "Not such products!Please input one item to buy again!"
                                    print "-----------------------------------------------------------------"
                                    choice = raw_input('please input one item to buy:')
                                    F_choice = choice.strip()
                                    continue
            break



    效果截图:


    164804liooypoxiztzstfa.jpg.thumb.jpg


    164805afh32yf4tvbbs6o3.jpg.thumb.jpg
    帖子永久地址: 

    黑帽联盟 - 论坛版权1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关
    2、本站所有主题由该帖子作者发表,该帖子作者与黑帽联盟享有帖子相关版权
    3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和黑帽联盟的同意
    4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
    5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
    6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
    7、黑帽联盟管理员和版主有权不事先通知发贴者而删除本文

    293

    主题

    18

    听众

    955

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

  • TA的每日心情
    奋斗
    2023-10-26 13:13
  • 签到天数: 358 天

    [LV.8]以坛为家I

    此购物系统还存在很多的Bug:

    1、此购物系统一次只能买一件物品,要求:一次可以买多件物品。

    2、当充值金额的时候,输入非数字的其它字符,会有异常(类型不匹配),会终结程序。要求:对此类异常进行处理,给予提示信息


    程序需要进一步完善:

    1、物品是我直接在程序里面定义好的,要求:把物品信息单独放在一个文件里面,比如:products.txt,然后通过这个程序来调用products.txt来获取物品信息

    第一点我已经解决,如果你还有其它方法的话,也可以。

    实现功能代码:

    首先在当前路径下创建一个products.txt文件,把物品信息填写进去。
    把这段代码放在最上面,然后把上面的两行代码删除掉,代码如下:
    objects = ['coffee','car','mobile','Internet','clothes']
    prices = [35,30000,3000,6000,500]

    实现功能代码:

    f = file('products.txt')
    products = []
    prices = []


    for line in f.readlines():
            new_line = line.split()
            products.append(new_line[0])
            prices.append(new_line[1])

    2、代码繁琐,重复太多,简化代码。
    回复

    使用道具 举报

    293

    主题

    18

    听众

    955

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

  • TA的每日心情
    奋斗
    2023-10-26 13:13
  • 签到天数: 358 天

    [LV.8]以坛为家I

    以下是其他用户优化的代码:
    1. #! /usr/bin/env python
    2. #encoding: utf-8

    3. #file: market.py
    4. #Author: toddlerya
    5. #date: 11-28-2014
    6. #version: 1.0

    7. """"
    8. 购物系统功能要求:

    9. 1、前提必须指定用户登录进去,才可进行购买!

    10. 2、登录进去之后,提示用户进行充值金额,否则无法进行购买物品!

    11. 3、充值成功后,提示用户是否进行购买(不购买直接退出系统)

    12. 4、用户同意购买之后,把所有产品列出供用户选择

    13. 5、针对之前用户充值的金额来判断用户哪些用户不能买,并给予提示信息;能购买的话,把物品加入购物框

    14. 6、如果用户输入的产品不存在,也给予相应的提示信息

    15. """

    16. objects = ['coffee','car','mobile','Internet','clothes']
    17. prices = [35,30000,3000,6000,500]
    18. shop_list = []

    19. name = "jojo"
    20. passwd = "bean"
    21. choose = ""
    22. choice = ""

    23. def login():
    24.     while True:
    25.         name = raw_input("please input your name:")
    26.         if name == "jojo":
    27.             password = raw_input("please input your password:")
    28.             while password != 'bean':
    29.                     password = raw_input("please input your password:")
    30.             print "----------------------------------"
    31.             print "welcome to login to system!\n"
    32.             print "----------------------------------"
    33.             break
    34.         else:
    35.             print "+-----------------------------+"
    36.             print "user %s not found,try again!" % name
    37.             print "+-----------------------------+"
    38.             continue

    39. def recharge():
    40.     global self_sal
    41.    
    42.     self_sal = (raw_input("please store your money: "))
    43.    
    44. while not self_sal.isdigit():
    45.     self_sal = raw_input('not a number, please try again: ')
    46.     self_sal = int(self_sal)</font>
    47.    
    48.     while True:
    49.         if self_sal > 0:
    50.             print '\n--------------------------------------'
    51.             print 'you had stored %s successfully!\n' % self_sal
    52.             break
    53.         else :
    54.             print '---------------------------------'
    55.             self_sal = int(raw_input("you store money failure,\nplease store money again:"))
    56.             continue

    57. def shop():
    58.     purchase = raw_input('Do you want to shop(yes/no):%s' % choose)
    59.     if purchase == 'yes':
    60.         for i in objects:
    61.                 print i,'\t',prices[objects.index(i)]
    62.     elif purchase == 'no':
    63.         print '\n------------------------------------'
    64.         print 'you had exited system successfully!'
    65.         print '----------------------------------'
    66.         global choice
    67.         choice = "!!!"
    68.         F_choice = choice.strip()

    69. login()
    70. recharge()
    71. shop()

    72.    
    73. while choice != "!!!":
    74.    
    75.     choice = raw_input("Please input one item to buy(input '!!!' to quit): ")
    76.     F_choice = choice.strip()
    77.     if F_choice == "!!!":
    78.         break
    79.     if F_choice in objects:
    80.         objects_index = objects.index(F_choice)
    81.         objects_price = prices[objects_index]
    82.         print F_choice,'---->',objects_price
    83.         if self_sal > objects_price:
    84.             shop_list.append(F_choice)
    85.             print 'Added %s into shop list' % choice
    86.             self_sal = self_sal - objects_price
    87.             print "Salary left:",self_sal
    88.             choice = ""
    89.             continue
    90.         else:
    91.             if self_sal < min(prices):
    92.                 print 'Sorry,rest of your salary cannot buy anything!88'
    93.                 print "you hava bought these things: %s" % shop_list
    94.                 break
    95.             else:
    96.                 print "Sorry,you cannot afford this product,please try other ones!\n"
    97.                 print "-----------------------------------------------------------------"
    98.                 choice = raw_input("please input one item to buy(input '!!!' to quit):")
    99.                 F_choice = choice.strip()
    100.                 continue
    101.     else:
    102.         print "Not such products!Please input one item to buy again!(input '!!!' to quit):"
    103.         print "-------------------------------------------------------"
    104.         choice = raw_input("please input one item to buy(input '!!!' to quit):")
    105.         F_choice = choice.strip()
    106.         continue









    复制代码
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 会员注册

    发布主题 !fastreply! 收藏帖子 返回列表 搜索
    回顶部