python3 购物车

  今天干了啥?喂了喂龟,看了看鱼。。。

然后就是学习了两个模块:sys模块和os模块,突然觉得python真的好,只要英语学的好,看代码超级舒服的说,嗯,我要好好学英语,今天背了几个啥,唉。写完博客再背几个。

  sys.path是用来显示环境变量的,环境变量是啥,有问题找百度,唉,懒得查。。。没救了。 反正用了sys.path可以打印出好多路径,貌似很多模块啥的都放在那。

  sys.argv是用来打印本python文件的相对路径的吧。。。不知道怎么用。什么运行程序时在文件名后面用空格隔开,打上1 2 3,在程序里print(sys.argv[2]) 还可以把2给输出出来,不知道有啥用。。。

  os.system("dir")会把目录输出到屏幕上,但是没有结果的,也就是不能把它存起来。想要存起来,就用os.popen("dir").read(),为啥要加read(),是因为前面那部分相当于只搞出了地址,得用read()把内容读出来。 os.mkdir("newdi")新建一个目录。

  有个小知识点,浮点数和小数是不一样的,查查百度,嗯,好吧,查了也还是不懂,反正两者又区别。。。

  然后就是有个三元运算,额a,b,c= 1,3,5    d=a if a>b else c  结果咧d= 5 。。。。。。不知道干啥用的,留个印象先。

  哦。。。 应该是这样,result = 值1  if 条件  else 值2    满足条件就赋值1,不然就赋值2.

  学个英文单词:encode,编码  decode,解码      用于string和bytes类型之间的转换  字符串和二进制之间的转换  encode(‘utf-8’)

学习列表:切片时中括号[a:b],冒号隔开。切片的时候,左开又闭不写就是默认开头结尾,可以正着数,也可以倒着来,比如-1就是最后一个。

list.append("object"),在列表末尾加一个       

list.insert(index,"object") 在索引处差入某值,只能一个一个的插。

list.index("object")  某个值的索引,就是下标。

list.reverse()   反转,就是把列表倒过来排。

list.count("object")  计数,看有多少个这样的值。

list.sort()   排序,从小到大按ASCII码来,特殊符号,先数字,在大写字母,再小写字母。

list.[index] = "object"  替换。

list.remove("object") ,del list[index], list.pop[index]    三种删除方式。 

list.clear()  清空列表。

list.extend(list_2)  合并列表。

list.copy()  是浅复制,就是说只能复制第一层,如果列表元素里有嵌套的话,那个改任何一个,复制的那个会一起变,因为嵌套的话,复制的是指针,要注意。

import copy   然后list_2 = copy.deepcopy(list)  是深复制,那两者之间就不会相互影响了。

 

在输入字符串时,要有一个判断:  if str.isdigit() :    这是用来判断输入的字符串是不是数字的字符串   这样判断以后再把它强制转换成int  不然没啥意义。

还有一个 for index, item in enumerate(list)     用来便利列表的下标和值  

元组,tuple  是一种一旦创建以后就不能修改的只读列表。

 

print打印时的一个骚操作:  高亮显示:   \033[31;1m%s\033[0m      31红色可换别的   %也可以换,其他为固定格式,想骚那就死背下来。。。

 

不早了记英文单词吧唉。。。字符串用法

capitalize  首字母大写 ,  center  放中间 ,     ljust   放左边, rjust  放右边,      endwith   判断是否以啥啥啥结尾,   find  查找,返回索引,     

isalnum  是不是字母和数字      isalpha    是不是纯数字     isidentifier  是不是合法标识符       islower  isupper  是不是大小写     isspace   是不是空格。。。

istitle    是不是每个单词首字母大写     lower  upper  全变小写,大写    replace("object", num)    替换j几个    split(“+”) 用+或别的把字符串分成列表  

swapcase   大变小写,小变大写      lstrip  strip  rstrip   取左边,两边,右边空格回车

 

重点:str=  “+”.join(["1","2","3"])     打印为 1+2+3

加密操作: p = str.maketrans("abcdefg", "1234567")

                   print("object".translate(p))     自己搞个秘文上去 哈哈哈哈

字典    dic.get("key")  取值,若没有,输出none

if object in dic   判断是否有object  (key)

dic.keys()  打印所有的key

dic.setdefault("key": "value")    先找,若没有就加进去

dic.update(dic_2)    key有交叉就覆盖,无则加

dic.items()     把字典变成列表

方法:    for i in dic:

                    print(i,dic[i])        省内存!

 

最后贴个作业 不多说 睡觉碎觉

一:

#!usr/bin/env/ python
# -*- coding:utf-8 -*-
# Author:XiaoFeng

dic = {"中国": {"湖北省": {"武汉市": ["武昌区", "洪山区"], "黄石市": "阳新县"},
              "四川省": {"成都市": "某某村"}},
       "美国": {"加州": {"某县": "某小镇"}}}

while True:
    for i in dic:
        print(i)
    choice1 = input("请输入想去哪1:")
    if choice1 in dic:
        for i1 in dic[choice1]:
            print("\t", i1)
        while True:
            choice2 = input("请输入想去哪2:")
            if choice2 in dic[choice1]:
                for i3 in dic[choice1][choice2]:
                    print("\t\t", i3)
                while True:
                    choice3 = input("请输入想去哪3:")
                    if choice3 in dic[choice1][choice2]:
                        for i4 in dic[choice1][choice2][choice3]:
                            print("\t\t\t", i4)
                        exit()
                    elif choice3 == "b":
                        break
                    else:
                        print("无此地点,请重新输入!")
                        continue
            elif choice2 == "b":
                break
            else:
                print("无此地点,请重新输入!")
                continue
    else:
        print("无此地点,请重新输入!")
        continue

二:  很抠脚  本来想存文件里的,还不会把字典存文件,唉。

 1 #!usr/bin/env/ python
 2 # -*- coding:utf-8 -*-
 3 # Author:XiaoFeng
 4 
 5 shopping_car = []
 6 list = [("Iphone", 8500),
 7         ("Mac Pro", 11200),
 8         ("Starebuck Latte", 31),
 9         ("Alex python", 81),
10         ("Bike", 800)]
11 
12 cmd = input("是否想要修改产品信息:")
13 if cmd == "y":
14     Name = input("商品名:")
15     Price = input("价格:")
16     list.append((Name, Price))
17 
18 file = open("balance", "r")
19 money = file.read()
20 file.close()
21 if money.isdigit():
22     money = int(money)
23 
24 if not money:
25     while True:
26         money = input("请输入您的余额:")
27         if money.isdigit():
28             file = open("balance", "a")
29             file.write(money)
30             file.close()
31             money = int(money)
32             break
33         else:
34             print("输入错误,请输入数字!")
35 
36 while True:
37     for index, item in enumerate(list):
38         print(index, item)
39 
40     user_choice = input("请输入您想要的选择:")
41     if user_choice.isdigit():
42         num = int(user_choice)
43         if num >= 0 and num < len(list):
44             if list[num][1] <= money:
45                 money = money - list[num][1]
46                 file = open("balance", "w")
47                 file.write(str(money))
48                 file.close()
49                 shopping_car.append(list[num])
50                 file = open("record", "a")
51                 file.write(str(list[num]))
52                 file.close()
53                 print("成功添加商品:\033[41;1m%s\033[0m,您的余额为:\033[32;1m[%d]\033[0m" % (list[num][0],money))
54             else:
55                 print("抱歉,您的余额为:\033[31;1m%s\033[0m,买不起哈哈哈!" % money)
56         else:
57             print("无此商品,请重新输入!")
58             continue
59     elif user_choice == "q":
60         print("--------shopping list--------")
61         for i in shopping_car:
62             print(i)
63         print("您的余额为:%d" % money)
64         exit()
65     else:
66         print("错误输入,请重新输入!")
67         continue

 

  

  

 

posted @ 2019-03-18 00:18  晓风小峰  阅读(438)  评论(0编辑  收藏  举报