2stDay

product_list =[('Iphonex',8300),
               ('Moto',6900),
               ('Mac Pro',18900),
               ('Latte',30),
               ('Mix',3999)]

yourSalary = input("Please print your Salary:")

shopping_list = []     #添加购物车
if yourSalary.isdigit():
    yourSalary = int(yourSalary)
    while True:
        '''
         1.列出商品编号,还有商品
        for item in product_list:
            print(product_list.index(item)+1,item)
        '''
        for index,item in enumerate(product_list):     #枚举方式
            print(index+1,item)
        your_choice = input("选择要买的商品>>>:")
        if your_choice.isdigit():
            your_choice = int(your_choice)-1
            if your_choice < len(product_list) and your_choice >= 0:#判断选择是否在范围内
                p_item = product_list[your_choice]    #选出选择的商品
                if p_item[1]<= yourSalary :           #买得起
                    shopping_list.append(p_item)#添加到购物车
                    yourSalary -=p_item[1]     #从卡里扣钱
                    print("Added %s into shopping car,your balance is "
                          "\033[31;1m%s\033[0m" %(p_item,yourSalary)) #  31--设置字体颜色
                else:
                    print("\033[41;1mYour last money just %s,go working!\033[0m"
                          %(yourSalary))#  %s相当于占位符,给%用   41--设置字体背景颜色
                # \033[41;1m************************\033[0m
            else:
                print ("Wrong written!")
        elif your_choice == 'q':
            print("------------shopping list-------------")
            for p in shopping_list:
                print(p)
            print("your Salary:",yourSalary)
            exit()
        else:
            print ("invalid")

else:
    print("Wrong Writen!")
购物
String
 1 name = "my name is jayChou"
 2 name.capitalize()  首字母大写
 3 name.casefold()   大写全部变小写
 4 name.center(50,"-")   输出50个字符,name内容在中间,剩余位置用“-”补充
 5 name.count('lex') 统计 lex出现次数
 6 name.encode()  将字符串编码成bytes格式
 7 name.endswith("Li")  判断字符串是否以 Li结尾
 8  "Alex\tLi".expandtabs(10) 输出'Alex      Li', 将\t转换成多长的空格 
 9  name.find('A')  查找A,找到返回其索引, 找不到返回-1 ,对字符串进行切片
10 name[name.find("name"):*]  [开始位置:结束位置]
11 format :
12     >>> msg = "my name is {}, and age is {}"
13     >>> msg.format("alex",22)
14     'my name is alex, and age is 22'
15     >>> msg = "my name is {1}, and age is {0}"
16     >>> msg.format("alex",22)
17     'my name is 22, and age is alex'
18     >>> msg = "my name is {name}, and age is {age}"
19     >>> msg.format(age=22,name="ale")
20     'my name is ale, and age is 22'
21 format_map
22     >>> msg.format_map({'name':'alex','age':22})
23     'my name is alex, and age is 22'
24 
25 
26 msg.index('a')  返回a所在字符串的索引
27 '9aA'.isalnum()   True
28 
29 '9'.isdigit() 是否整数
30 name.isnumeric  
31 name.isprintable
32 name.isspace
33 name.istitle
34 name.isupper
35  "|".join(['alex','jack','rain'])
36 'alex|jack|rain'
37 
38 
39 maketrans
40     >>> intab = "aeiou"  #This is the string having actual characters. 
41     >>> outtab = "12345" #This is the string having corresponding mapping character
42     >>> trantab = str.maketrans(intab, outtab)
43     >>> 
44     >>> str = "this is string example....wow!!!"
45     >>> str.translate(trantab)
46     'th3s 3s str3ng 2x1mpl2....w4w!!!'
47 
48  msg.partition('is')   输出 ('my name ', 'is', ' {name}, and age is {age}') 
49 
50  >>> "alex li, chinese name is lijie".replace("li","LI",1)
51      'alex LI, chinese name is lijie'
52 
53  msg.swapcase 大小写互换
54 
55 
56  >>> msg.zfill(40)
57 '00000my name is {name}, and age is {age}'
58 
59 
60 
61 >>> n4.ljust(40,"-")
62 'Hello 2orld-----------------------------'
63 >>> n4.rjust(40,"-")
64 '-----------------------------Hello 2orld'
65 
66 
67 >>> b="ddefdsdff_哈哈" 
68 >>> b.isidentifier() #检测一段字符串可否被当作标志符,即是否符合变量命名规则
69 True

 


 1 name.capitalize()  首字母大写
 2 name.casefold()   大写全部变小写
 3 name.center(50,"-")  输出 '---------------------Alex Li----------------------'
 4 name.count('lex') 统计 lex出现次数
 5 name.encode()  将字符串编码成bytes格式
 6 name.endswith("Li")  判断字符串是否以 Li结尾
 7  "Alex\tLi".expandtabs(10) 输出'Alex      Li', 将\t转换成多长的空格 
 8  name.find('A')  查找A,找到返回其索引, 找不到返回-1 
 9 
10 format :
11     >>> msg = "my name is {}, and age is {}"
12     >>> msg.format("alex",22)
13     'my name is alex, and age is 22'
14     >>> msg = "my name is {1}, and age is {0}"
15     >>> msg.format("alex",22)
16     'my name is 22, and age is alex'
17     >>> msg = "my name is {name}, and age is {age}"
18     >>> msg.format(age=22,name="ale")
19     'my name is ale, and age is 22'
20 format_map
21     >>> msg.format_map({'name':'alex','age':22})
22     'my name is alex, and age is 22'
23 
24 
25 msg.index('a')  返回a所在字符串的索引
26 '9aA'.isalnum()   True
27 
28 '9'.isdigit() 是否整数
29 name.isnumeric  
30 name.isprintable
31 name.isspace
32 name.istitle
33 name.isupper
34  "|".join(['alex','jack','rain'])
35 'alex|jack|rain'
36 
37 
38 maketrans
39     >>> intab = "aeiou"  #This is the string having actual characters. 
40     >>> outtab = "12345" #This is the string having corresponding mapping character
41     >>> trantab = str.maketrans(intab, outtab)
42     >>> 
43     >>> str = "this is string example....wow!!!"
44     >>> str.translate(trantab)
45     'th3s 3s str3ng 2x1mpl2....w4w!!!'
46 
47  msg.partition('is')   输出 ('my name ', 'is', ' {name}, and age is {age}') 
48 
49  >>> "alex li, chinese name is lijie".replace("li","LI",1)
50      'alex LI, chinese name is lijie'
51 
52  msg.swapcase 大小写互换
53 
54 
55  >>> msg.zfill(40)
56 '00000my name is {name}, and age is {age}'
57 
58 
59 
60 >>> n4.ljust(40,"-")
61 'Hello 2orld-----------------------------'
62 >>> n4.rjust(40,"-")
63 '-----------------------------Hello 2orld'
64 
65 
66 >>> b="ddefdsdff_哈哈" 
67 >>> b.isidentifier() #检测一段字符串可否被当作标志符,即是否符合变
posted @ 2017-09-20 20:13  Yif.Z  阅读(325)  评论(0)    收藏  举报