python学习-day1-包含部分基本知识点

17年4月中旬在51cto上报名了老男孩PYTHON高级自动化开发的课程,此随笔为记录学习中的有意义的东西(自我感觉有意义)

一、pycharm的基本配置与使用

之前自学过一段时间,一直用的自带的IDLE和notepad++,现在开始用pycharm,但是很不熟悉,随着使用不断补充和修改

开始可以设置字体、样板等,网上都很容易搜到

1.setting--editor--file and code template ,找到Python script,可以为新建的Python文件定义模板,只有再次新建才会生效。

2.常用快捷键:

ctril + D 复制当前行

ctril + Y 删除当前行

ctril + / 为当前行注释/取消注释

TAB,为当前选中部分都增加一个TAB,相反的话,SHIFT + TAB

二、基本知识点

2.1 input()默认输入的都是字符串格式,如果输入数字需要比较大小的话,可以int(input())

2.2 Python 2.x如果需要使用中文的话,需要声明# -*- coding: utf-8 -*- 因为默认使用的是ASCII

2.3 continue 和 break

 1 for i in range(10):
 2 
 3     if i<5:
 4         continue #不往下走了,直接进入下一次loop
 5     print("loop:", i )
 6 
 7 ####################################
 8 for i in range(10):
 9     if i>5:
10         break #不往下走了,直接跳出整个loop
11     print("loop:", i )

2.4 注释

两种注释,一个是每行内容前加#,#后面的这一行内容就被注释了  (常用语解释每行代码什么意思)

另一种是使用三重引号,里面添加内容,""" 内容""",如果没有将它赋予一个变量,那么三重引号之间的内容就是一个注释(写函数,模块或者文件说明时常用)

2.5 打印print()

默认每一条print语句后面都打印换行符,如果print后面替换为制表符

print(' ',end='')或者print('打印的内容',end=' ')

def print(self, *args, sep=' ', end=' ', file=None):

sep表示print打印的多个value默认是以空格相隔,end默认以回车结尾,file = None,其实就是默认当前输出为sys.stdout,标准输出当前屏幕上,所以file = file_name,输出到文件中

b =123
a = 'sfaf'
with open('copy1','w') as f:
    print(a,b,a,sep='----',file=f)

#屏幕上不会有输出,会输出到文件copy1上
文件显示如下:
sfaf----123----sfaf  #默认是以空格间隔,现在是----

 2.6 locals()

内置函数,返回当前作用域中的变量集合

 

三、 while、for循环的例子:

猜年龄,猜对了,跳出整个循环,共3次机会,都错了,提示是否继续,否的话,直接推出。根据语境想了三个例子:

第一个是视频上讲的,最简洁
cq_age = 27
n = 0
while n < 3:    #只要n小于3,也就是0,1,2,三次都会执行while,否则执行最后的else.
    n +=1
    age = int(input("please enter your guess:"))
    if age < cq_age:
        print("think bigger")
    elif age > cq_age:
        print("thinker smaller")
    else:
        print("you got it")
        break       #跳出整个while循环,属于非正常执行完while,所以不会执行最后的else
    if n == 3:
        answer = input("do you wangt conntinue ?")
        if answer !=  'n': #只要你不回答n,默认同意继续,令变量n再次赋值为0,如果是n的话,那么n==3,循环就为False了,执行最后的else
            n = 0
else:
    print("you have tried 3 times ,fuck off")

第二个是未看视频前,自己琢磨的,好low:
 1 cq_age = 27
 2 n = 0
 3 while n == 0:           #大循环,n只要等于0为真,这个巡检就一直执行
 4     while n < 3:        #小循环
 5         n +=1
 6         age = int(input("please enter your guess:"))
 7         if age < cq_age:
 8             print("think bigger")
 9         elif age > cq_age:
10             print("thinker smaller")
11         else:
12             print("you got it")
13             break       #break会跳出当前的循环,这里只跳出小循环,所以后面if语句判断年龄猜对的话,继续break大循环,结束,不然只跳出小循环的话,大循环n等于0接着不满足条件还要执行最后的else,不合要求
14     else:
15         answer = input("you have tried 3 times ,do you want continue ?")
16         if answer != 'n':
17             n = 0           #保证大循环的n等于0条件仍然满足,继续大循环一次,那么小循环也会继续执行
18         #else:
19          #   break
20     if age == cq_age:           #对应上面的第一个break,
21         break
22 else:
23     print("bye ,lowerB")

用for写的
 1 cq_age = 27
 2 n = 0
 3 while True:             #试了3次且答复不继续或答对了才会退出,否则一直123的循环
 4     for n in range(3):
 5         age = int(input("please enter your guess:"))
 6         if age < cq_age:
 7             print("think bigger")
 8         elif age > cq_age:
 9             print("thinker smaller")
10         else:
11             print("you got it")
12             break
13     else:
14         answer = input("you tried 3 times,do you want continue ?")
15         if answer == 'n':
16             print('bye')
17             break       #此break对应的是紧邻的else,不在for循环内,所以跳出的是while循环
18     if age == cq_age:
19         break   #也是跳出while循环

四、作业:

4.1 编写登陆接口
  • 输入用户名密码
  • 认证成功后显示欢迎信息
  • 输错三次后锁定(就是锁定后,再登录,就提示用户已经锁定。所以每次登陆前都要做一次是否锁定的检查)

大体思路:用户名:密码存在一个字典user_pass里,锁定的用户存在一个列表lock_user里,输入用户名后,检查用户名是否在lock_user里,在的话,提示被锁定,退出;没有锁定的话,检查用户名是否在

字典里,不在的话提示无效的用户名,在的话再输入密码,验证三次不成功,将用户添加到lock_user列表里。之后用文件替代这些表,每次运行先读取这些文件。

 1 #########start:create two files named userpass and lockuser###########
 2 # file1 = open('userpass.txt','w')
 3 # file1.write("{'cui qing':'abc123','zhangsan':'abc111','lisi':'aaa123','wangwang':'123ert','ppp':'qwer'}")
 4 # file1.close()
 5 # print(user_pass)
 6 # file2 = open('lockuser.txt','w')
 7 # file2.write("['zhangsan','lisi']")
 8 # file2.close()
 9 #############end################################################
10 file1 = open('userpass.txt','rt')
11 line1 = file1.read()
12 user_pass = eval(line1)
13 file2 = open('lockuser.txt','rt')
14 line2 = file2.read()
15 lock_user = eval(line2)       #eval()可以将字符串转换为表达式,因为从文本读取的是一个str格式,e转换后变成dict
16 # user_pass = {'cui qing':'abc123','zhangsan':'abc111','lisi':'aaa123'}
17 # print (user_pass['cui qing'])
18 # lock_user = ['zhangsan','lisi']
19 # print(lock_user)
20 n = 0
21 while True:
22     username = input("please enter your username:")
23     if username in lock_user:
24         print('your account is locked!')
25         break           #退出while true循环
26     elif username in user_pass:
27         print('your account is true')
28         while n < 3:
29             n += 1
30             password = input('enter your password:')
31             if password == user_pass[username]:
32                 print("welcome %s"%username)
33                 break           #退出小循环
34             else:
35                 print("your password is wrong")
36         else:
37             print('''wrong password
your account is locked.''')
38             lock_user.append(username)
39             lock_user_str = str(lock_user)
40             file2 = open('lockuser.txt', 'w')
41             file2.write(lock_user_str)
42             file2.close()
43             print (lock_user)
44             break           #退出while true循环
45         if password == user_pass[username]:
46             break           #退出while true循环
47     else:
48         print('invalid username')
4.2 多级菜单
  • 三级菜单
  • 可依次选择进入各子菜单(目前有问题,先抄上,到公司去研究下)

# f = open('hw3.txt','w')
# f.write("""{'上海':{'闵行':('莘庄','七宝','春申'),'浦东':('陆家嘴','张江')},'山东':{'济南':('历下区','槐荫区'),'德州':('齐河县','禹城市')}}""")
# f.close()

f = open('hw3.txt','rt')
memu = eval(f.read())
f.close()
# print(memu)
top = True
print('输入名称进入下一级,quit退出,back返回')
while top:
for memu1 in memu:
print(memu1)
answer1 = input('L1:')
if answer1 in memu:
while top:
for memu2 in memu[answer1]:
print(memu2)
answer2 = input('L2:')
if answer2 in memu[answer1]:
while top:
for memu3 in memu[answer1][answer2]:
print(memu3)
answer3 = input('L3:')
if answer3 in memu[answer1][answer2]:
print('Welcome to %s'%answer3)
top = False
elif answer3 == 'quit':
top = False
elif answer3 == 'back':
break
elif answer2 == 'quit':
top = False
elif answer2 == 'back':
break
elif answer1 == 'quit':
top = False
 
原文地址:https://www.cnblogs.com/cq90/p/6748659.html