刚开始学Python一周,主要掌握程序开发的基本知识:
1 变量赋值与引用
2 条件控制语句,如if。。。else,for循环,while循环等
3 数组,元组,字典基本概念与操作(增,删,改。查),感觉数组与字典在python开发里面会被大量使用
4 基本模块os,当前主要了解os.getcwd(),获取当前路径以及os.chdir()切换路径
5 基本模块sys,主要了解sys.argv获取参数数量
6 文件基本操作以及对应模式
代码示例1:
模拟用户登录,主要包括:验证失败锁定账号+账号注册+密码修改+查看账号+创建账号
#!/usr/bin/env python# -*- coding: utf-8 -*- user_allow = []user_deny = [] while True: banner = "Welcome" print(banner.center(20,"=")) if user_allow: for i in range(3): banner_login = "Please enter your name and password to login!" print(banner_login) lname = input("Login Name:") lpwd = input("Login Password:") if user_deny: for m in range(len(user_deny)): if lname == user_deny[m][0]: print("It is locked. Please contract your administrator !") else: pass for n in range(len(user_allow)): if lname == user_allow[n][0] and lpwd == user_allow[n][1]: print("Welcome %s to login !" % lname) select = input("List[l]| Reset password [c]| Users[a]| Logout[x]:") if select == "l": for luser in range(len(user_allow)): print(user_allow[luser][0]) break elif select == "x": print("Logout! Bye bye !") break elif select == "c": print("Now reseting the password...") opwd = input("Your old password:") npwd = input("Your new password:") npwd2 = input("Confirm:") if npwd == npwd2: if opwd == user_allow[n][1]: user_allow[n][1] = npwd2 print("The password is updated!") else: print("It is not match !") continue elif select == "a": print("==Add new user info==") add_user = input("The new account:") add_pwd1 = input("The password:") add_pwd2 = input("Confirm:") if add_user not in user_allow and add_pwd1 == add_pwd2: user_allow.append([add_user,add_pwd2]) print("The user is added.") continue else: print("It is not match!") continue else: i = 2 - i if i > 0 and i <=2 : print("Sorry,you have %s times" % i) elif i == 0: print("Sorry,your account is locked!") user_deny.append([lname,lpwd]) exit() else: print("Please register first!") uname = input("New Name:") upwd = input("New Password:") user_allow.append([uname,upwd]) print("The register is successful !")
示例2:
3级菜单,主要包括显示菜单列表+退出+返回功能:
#!/usr/bin/env python# -*- coding: utf-8 -*- menu = { "servers":{ "HP":{ "G9":["2C/32T","256G","1T*8",50000] } }, "labtops":{ "IBM":{ "T560":["1C/8T","8G","128SSD*1+1T*1",12000] } }, "networks":{ "CISCO":{ "C2950":["48P",6000] } }} while True: for i in menu: print(i) select1 = input("Your select[Quit:q]:") #servers if select1 in menu: while True: for i2 in menu[select1]: print("\t",i2) select2 = input("Your select[Back:x|Quit:q]:") #HP if select2 in menu[select1]: while True: for i3 in menu[select1][select2]: print("\t\t",i3) select3 = input("Your select[Back:x|Quit:q]:") # G9 if select3 in menu[select1][select2]: while True: for i4 in menu[select1][select2][select3]: print("\t\t\t",i4) select4 = input("[Back:x|Quit:q]:") if select4 == "x": break elif select4 == "q": exit() elif select3 == "x": break elif select3 == "q": exit() elif select2 == "x": break elif select2 == "q": exit() elif select1 == "q": exit()
感受:
以前一直习惯用shell写脚本,在功能比较简单的情况下,还是喜欢shell的简单粗暴,单刀直入。特别接近系统层面的操作,读取文件,过滤字符串等操作。
Python没有像shell那样的grep过滤功能,很多判断主要依靠in语句。其也没有case语句,可以构建菜单,目前主要通过字典方式形成。
刚开始写shell脚本时,通常命令比较熟悉,但是思路欠缺。而现在学python开发时,往往有思路,但是通过何种功能实现,比较陌生。
相信经过一段时间练习,熟悉的python的特性,掌握更多的操作命令,开发更复杂的程序会更加得心应手。
时间: 2024-10-03 09:50:24