day1(老男孩-Python3.5-S14期全栈开发)

作者:赵俊            发布日期:2019/10/18

一、第一个python程序

1、在解释器下写hello world程序运行,与运行外部文件方法

 运行外部文件,必须在相应位置创建一个python文件,里面写上语句

 2、#!/usr/bin/evn python的作用,告诉操作系统使用的解释器是什么

#!/usr/bin/python相当于写死了python路径;
#!/usr/bin/env python会去环境设置寻找python目录,推荐这种写法

二、变量

1、pycharm工程新建文件,模板代码设置

 2、变量的内存管理

 

 

 3、变量定义的规则

  • 变量只能是字母、数字或下划线的任意组合
  • 变量的第一个字符不能是数字
  • 以下关键字不能声明为变量

   ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise',   'return', 'try', 'while', 'with', 'yield']

 

三、字符编码的区别与介绍

1、python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ascill)

ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言,其最多只能用 8 位来表示(一个字节),即:28 = 256,所以,ASCII码最多只能表示 255 个符号。

 

 2、关于中文编码

简体中文的GB2312和用于繁体中文的big5

从ASCII、GB2312、GBK 到GB18030,这些编码方法是向下兼容的

3、国际编码

Unicode(统一码、万国码、单一码)是一种在计算机上使用的字符编码

UTF-8,是对Unicode编码的压缩和优化,他不再使用最少使用2个字节,而是将所有的字符和符号进行分类:ascii码中的内容用1个字节保存、欧洲的字符用2个字节保存,东亚的字符用3个字节保存

4、# -*- coding: utf-8 -*-

python 2.x上写中文要告诉解释器编码格式

python 3.x支持unicode编码

四、用户输入及格式化输出

1、注释

单行注释:#号后面跟要注释的内容

多行注释:'''被注释内容'''                三个单引号或双引号都可以

多行注释可以用来打印多行,如下图,左侧为源码,右侧为输出

 

 2、用户输入

1 username = input("请输入用户名")
2 password = input("请输入密码")
3 print(username,password)

3、格式化输出

用+号拼接字符串,不建议使用,占用内存多

1 name = input("请输入用户名")
2 age = input("请输入年龄")
3 info = '''
4 ----------info of '''+name+'''----------
5 name:'''+name+'''
6 age:'''+age
7 print(info)

用%s或%d或f%,%s是接收字符串的,%d是接收数值的,%f是接收浮点。input输入的均为字符串,所有在用%d时,必须用int()强制类型装换为整型

1 name = input("请输入用户名")
2 age = input("请输入年龄")
3 info = '''
4 ----------info of %s----------
5 name:%s
6 age:%s
7 '''%(name,name,age)
8 print(info)

 使用format格式化输出

1 name = input("请输入用户名")
2 age = input("请输入年龄")
3 info = '''
4 ----------info of {_name}----------
5 name:{_name}
6 age:{_age}
7 '''.format(_name=name,_age= age)
8 print(info)

 使用format格式化输出另一种

1 name = input("请输入用户名")
2 age = input("请输入年龄")
3 info = '''
4 ----------info of {0}----------
5 name:{0}
6 age:{1}
7 '''.format(name,age)
8 print(info)

控制台输出文字带颜色

说明:
前景色         背景色           颜色
---------------------------------------
30                40              黑色
31                41              红色
32                42              绿色
33                43              黃色
34                44              蓝色
35                45              洋红
36                46              青色
37                47              白色
显示方式               意义
----------------------------------
0                    终端默认设置
1                    高亮显示
22           非高亮显示
4 使用下划线
24           去下划线
5 闪烁
25           去闪烁
7 反白显示
27           非反显
8 不可见
28           可见
语法格式
\033[1;31;40m: 代表接下来输出内容为:高亮显示,前景色为红色,背景色被黑色。[ 与 m 之间参数的顺序可以颠倒,可以省略

\033[0m:代表接下来的输入内容为终端默认设置,也就是取消之前的颜色设置,如果没有这个,接下来的输出,都是上面的设置。


例子:
print('\033[1;31;40m被修饰的字符串\033[0m')
\033[1;31;40m    <!--1-高亮显示 31-前景色红色  40-背景色黑色-->
\033[0m          <!--采用终端默认设置,即取消颜色设置--> 

 

五、if else流程判断

1、密文输入

导入标准库import getpass。这个在pychar中没效果,用命令行就可以实现

1 import getpass
2 name = input("请输入用户名")
3 age = getpass.getpass("请输入年龄")
4 info = '''
5 ----------info of {0}----------
6 name:{0}
7 age:{1}
8 '''.format(name,age)
9 print(info)

 

 2、if语句基本结构

1 _username = "zhaojun"
2 _password = "123"
3 username = input("请输入用户名:")
4 password = input("请输入密码:")
5 
6 if _username == username and _password == password:
7     print("welcome {name} login...".format(name = _username))
8 else:
9     print("invalid username or password")

3、if elif语句      猜数字

1 _number = 33
2 number = int(input("猜数字:"))
3 
4 if number == _number:
5     print("恭喜你,猜对了!")
6 elif number > _number:
7     print("猜大了!")
8 else:
9     print("猜小了!")

 

六、while循环

1、while循环,改进循环猜数字3次

 1 _number = 33
 2 count = 0
 3 while count<3:
 4     number = int(input("猜数字:"))
 5     if number == _number:
 6         print("恭喜你,猜对了!")
 7         break
 8     elif number > _number:
 9         print("猜大了!")
10     else:
11         print("猜小了!")
12     count +=1

2、while else。改进三次后提示猜次数过多

 1 _number = 33
 2 count = 0
 3 while count<3:
 4     number = int(input("猜数字:"))
 5     if number == _number:
 6         print("恭喜你,猜对了!")
 7         break
 8     elif number > _number:
 9         print("猜大了!")
10     else:
11         print("猜小了!")
12     count +=1
13 else:
14     print("已经猜了三次了,再见!")

 3、猜数字任性玩改进

 1 _number = 33
 2 count = 0
 3 while count<3:
 4     number = int(input("猜数字:"))
 5     if number == _number:
 6         print("恭喜你,猜对了!")
 7         break
 8     elif number > _number:
 9         print("猜大了!")
10     else:
11         print("猜小了!")
12     count +=1
13     if count == 3:
14         confirm = input("你是否继续猜数字?按回车重新开始,按n为退出")
15         if confirm != "n":
16             count = 0

 

七、for循环

 1、基本语法

1 for i in range(10):
2     print("loop",i)

2、用for循环改进猜数字

 1 _number = 33
 2 for i in range(3):
 3     number = int(input("猜数字:"))
 4     if number == _number:
 5         print("恭喜你,猜对了!")
 6         break
 7     elif number > _number:
 8         print("猜大了!")
 9     else:
10         print("猜小了!")
11 else:
12     print("已经猜了三次了,再见!")

 3、range里面的参数分别为,最小值,最大值,步长

1 for i in range(0,10,1):
2     print("loop",i)

 

 八、课后作业

1、作业二:编写登陆接口

  • 输入用户名密码
  • 认证成功后显示欢迎信息
  • 输错三次后锁定

在代码目录建立三个文本文件如下(最后一个字符串要带换行,因为前几行都有换行,为了比较时好比较,也就给最后一个加换行):

 

程序流程图:

 代码:

 1 # 是否锁定标志
 2 is_locked = 0
 3 # 登录三次计数
 4 count = 0
 5 #用户名是否存在标志
 6 user_exist = 0
 7 num = 0
 8 while count<3:
 9     print("----------请登录----------")
10     username = input("请输入用户名:")
11     password = input("请输入密码:")
12     count+=1
13     locked_file = open("locked.txt", "r")
14     locked_list = locked_file.readlines()
15     for i in range(len(locked_list)):
16         if username + "\n" == locked_list[i]:
17             is_locked = 1
18             break
19     if is_locked == 0:
20         locked_file.close()
21         user_file = open("username.txt", "r")
22         user_list = user_file.readlines()
23         for i in range(len(user_list)):
24             if username+"\n" == user_list[i]:
25                 user_exist = 1
26                 num = i
27                 break
28         if user_exist == 1:
29             user_file.close()
30             pass_file = open("password.txt", "r")
31             pass_list = pass_file.readlines()
32             if password + "\n" == pass_list[num]:
33                 pass_file.close()
34                 print("恭喜,登录成功!")
35                 break
36             else:
37                 pass_file.close()
38                 print("密码错误!")
39                 continue
40         else:
41             user_file.close()
42             print("用户名不存在,请从新输入!")
43     else:
44         locked_file.close()
45         print("用户被锁定,请联系管理员解锁!")
46 else:
47     locked_file = open("locked.txt", "a")
48     locked_file.write(username+"\n")
49     locked_file.close()
50     print("输入超过三次,用户被锁定!")

 

2、作业三:多级菜单
  • 三级菜单
  • 可依次选择进入各子菜单
  • 所需新知识点:列表、字典

程序流程图:

 

 

代码:

 1 #Author:ZHJ
 2 phone_model_huawei = {
 3     "Mate30系列":""
 4     ,"nova 5 Pro系列":""
 5     ,"畅享9系列":""
 6 }
 7 phone_model_apple = {
 8     "iPhone 11系列":""
 9     ,"iPhone X系列":""
10     ,"iPhone 8系列":""
11 }
12 phone_model_samsung = {
13     "GALAXY Note 10系列":""
14     ,"GALAXY Note 9系列":""
15     ,"GALAXY S8系列":""
16 }
17 
18 computer_modle_lenove = {
19     "启天M415系列":""
20     ,"扬天T4900d系列":""
21     ,"擎天T510A系列":""
22 }
23 computer_modle_dell = {
24     "成铭 3980MT系列":""
25     ,"Inspiron 灵越 3670系列":""
26     ,"Vostro 成就 3670系列":""
27 }
28 computer_modle_hp = {
29     "光影精灵系列":"",
30     "暗影精灵5系列":"",
31     "战99 Pro G1 MT系列":""
32 }
33 
34 phone_brand = {
35     "华为":phone_model_huawei,
36     "苹果":phone_model_apple,
37     "三星":phone_model_samsung
38 }
39 computer_brand = {
40     "联想":computer_modle_lenove,
41     "戴尔":computer_modle_dell,
42     "惠普":computer_modle_hp
43 }
44 
45 commodity = {
46     "手机":phone_brand,
47     "电脑":computer_brand
48 }
49 
50 num = 1
51 input_commodity = ""
52 input_brand = ""
53 input_modle = ""
54 while True:
55     if num == 1:
56         print("----------请输入要查询的商品----------")
57         print(list(commodity.keys()))
58         input_commodity = input("请选择一个商品:")
59         if input_commodity in commodity:
60             num += 1
61         elif input_commodity =="q":
62             break
63         elif input_commodity =="b":
64             continue
65         else:
66             print("查询商品不存在,请重新输入!")
67             continue
68     elif num == 2:
69         print("----------请输入要查询的品牌----------")
70         print(list(commodity.get(input_commodity).keys()))
71         input_brand = input("请选择一个品牌:")
72         if input_brand in commodity.get(input_commodity):
73             num += 1
74         elif input_brand =="q":
75             break
76         elif input_brand =="b":
77             num-=1
78             continue
79         else:
80             print("查询品牌不存在,请重新输入!")
81             continue
82     elif num == 3:
83         print("----------请输入要查询的型号----------")
84         print(list(commodity.get(input_commodity).get(input_brand).keys()))
85         input_modle = input("请选择一个型号:")
86         if input_modle in commodity.get(input_commodity).get(input_brand):
87             print("----------**********----------")
88             print("谢谢使用,查询完成!")
89             print("----------**********----------")
90             break
91         elif input_modle =="q":
92             break
93         elif input_modle =="b":
94             num-=1
95             continue
96         else:
97             print("查询品牌不存在,请重新输入!")
98             continue

 

posted @ 2019-10-18 10:39  daban2009  阅读(491)  评论(2编辑  收藏  举报