第一個python小實驗
前言
作為一個工作1年的linux運維搬磚師來說,發現沒點開發能力真的是不好混啊。於是下定決心學習python!
直接上剛寫的語句(大神莫鄙視)
通過控制台輸入一個賬號密碼,若輸入的賬號密碼是定義的則true,若錯誤將錯誤密碼保存到用戶、密碼文件
import getpass user='yang' #定義一個用戶 password='123' #定義一個密碼 userfile='fuser' #存用戶名的文件 pasfile='fpas' #存密碼的文件 createfile1=open(userfile,'a+') #創建文件 createfile2=open(pasfile,'a+') inputuser=input("輸入用戶名:") inputpassword=getpass.getpass("輸入密碼:") if inputuser == 'yang' and inputpassword == '123': print("true") else: print(inputuser) createfile1.write(inputuser + '\n') print(inputpassword) createfile2.write(inputpassword + '\n')
擴展延伸
getpass模塊 :作用是在輸入密碼是可以“隱藏”,類似於linux登入是輸入password效果一樣
open()函數:用於打開一個文件,創建個file對象。(https://www.runoob.com/python/python-func-open.html)