如何写一份购物车系统?

# Author Eric feng

product_list = [
        ("Iphone", 5800),
         ("Mac pro", 9800),
        ("Eric book", 58),
         ("Case of US", 1000),
]
shopping_list = []
salary = Input("input your salary:")
if salary.isdigit():
   salary = int(salary)
   while Ture:
       for index, item in enumerate(product_list):
          print(index, item)
       user_choice = input(user_choice):
       if user_choice.isdigit():
          user_choice = int(user_choice)
          if user_choice < len(product_list) and user_choice >=0:
              p_item = product_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%\033[0m"%(p_item,salary))
                 else:
                   print("\033[41:1m你的余额只剩[%s]啦,还买个毛线、033【0m"%salary)
                 else:
                    print("product code[%s] is not exit!"% 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")

一:首先需要产品列表

二:薪水计算

三:isdigit的定义

四:enumerate()是python的内置函数
enumerate在字典上是枚举,列举的意思
对于一个可迭代的(iterable)/可遍历的对象(如列表,字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值

五:append表达的含义:

切片含义表示:-1:从后往前依次进位可表示 -1:-2:-3。不同的数表示just  print(name[-1, -3])此为切片。
names.append("LeiHaidong")
names.insert(1,"Chengronghua")
sys模块是C语言使用的,所以不可能像OS一样找到OS的文件

五:while True的判断,买得起和买不起的语句选择

六:之后结束命令语句

 一、isdigit()

python关于 isdigit() 内置函数的官方定义: S.isdigit() -> bool                 Return True if all characters in S are digits         and there is at least one character in S, False otherwise. 翻译: S.isdigit()返回的是布尔值:True False S中至少有一个字符且如果S中的所有字符都是数字,那么返回结果就是True;否则,就返回False 
复制代码
 1 S1 = '12345'       #纯数字
 2 S2 = '①②'        #带圈的数字
 3 S3 = '汉字'        #汉字
 4 S4 = '%#¥'        #特殊符号
 5 
 6 print(S1.isdigit())
 7 print(S2.isdigit())
 8 print(S3.isdigit())
 9 print(S4.isdigit())
10 
11 # 执行结果:
12 True     
13 True
14 False
15 False
复制代码

 二、isalpha()

python关于 isalpha() 内置函数的官方定义: S.isalpha() -> bool                 Return True if all characters in S are alphabetic         and there is at least one character in S, False otherwise. 翻译: S.isalpha()返回的是布尔值:True False S中至少有一个字符且如果S中的所有字符都是字母,那么返回结果就是True;否则,就返回False
复制代码
 1 S1 = 'abc汉字'     #汉字+字母
 2 S2 = 'ab字134'     #包含数字
 3 S3 = '*&&'         #特殊符号
 4 
 5 print(S1.isalpha())
 6 print(S2.isalpha())
 7 print(S3.isalpha())
 8 
 9 #执行结果
10 True
11 False
12 False
复制代码
三、isalnum()
python关于 isalnum() 内置函数的官方定义: S.isalnum() -> bool          Return True if all characters in S are alphanumeric         and there is at least one character in S, False otherwise. 翻译: S.isalnum()返回的是布尔值:True False S中至少有一个字符且如果S中的所有字符都是字母数字,那么返回结果就是True;否则,就返回False
复制代码
 1 S1 = 'abc汉字1'    #字母+汉字+数字
 2 S2 = '①②③'      #带圈的数字
 3 S3 = '%……&'       #特殊符号
 4 
 5 print(S1.isalnum())
 6 print(S2.isalnum())
 7 print(S3.isalnum())
 8 
 9 #执行结果
10 True
11 True
12 False
posted @ 2018-09-08 16:47  蓝胖子之心  阅读(249)  评论(0编辑  收藏  举报