標籤:print 知識 列印 pass 是什麼 英文 互動 ... 補全
python3.5使用
第四天:
上次學了比較深入的輸入輸出寫法,這次我們學習的是流程判斷if else我們用上次的練習互動程式來學習它們(順便再練習一遍)
1 username=input("username:")2 password=input("password:")3 print(username,password)
我們登入的時候,需要使用者名稱和密碼,那麼我們的程式是不是就要判斷輸入的使用者名稱和密碼正不正確。
這個時候就用到了if(如果)
我們要知道使用者名稱和密碼正不正確,那麼我們就必須要先在電腦中儲存這個資料對不對
那麼我們就申請兩個變數,一個存使用者名稱一個存密碼
1 _username=‘huang‘2 _password=‘1234‘3 username=input("username:")4 password=input("password:")5 print(username,password)
然後就是這樣了(都教過了)
接下來就是真正重要的知識了if,if是什麼呢是如果的意思,比如說如果a等於b那麼就怎麼樣
補充一個小知識在編程裡面一個等號‘=’意思是賦給某某(就是把他的東西複製一份給其他),兩個等號‘==’才是等於的意思
and(和)英文和中文意思,and的使用是什麼和什麼合在一起
如果(if)程式存的使用者名稱(_username)等於(==)你輸入的使用者名稱 和(and)程式存的密碼(_password)等於(==)你輸入的密碼:
print(列印)(‘Landfall...(登陸中。。。)‘)
完整代碼如下:
1 _username=‘huang‘2 _password=‘1234‘3 username=input("username:")4 password=input("password:")5 print(username,password)6 if _username==username and _password==password:7 print(‘Landfall...‘)
一些小細節:
if(如果)(條件):這個冒號非常重要條件結束要加上,必須是英文冒號
執行的命令要縮排(縮排是一個tab鍵,tab鍵還有一個功能是補全例如你打一個你寫過一遍的變數開啟頭幾個字母按tab鍵會自動補全)
else的使用:
我們接著上面的代碼來講else(否則)else這個一般不會單獨出現必定是與if成對出現
你看如果我的密碼輸入的不對那麼(else)就執行其他命令,因為這裡的if與else是平級的所以else不用縮排,但else下執行的命令需要縮排。
比如說不對就列印你輸入的使用者名稱或密碼有錯(The username or password you entered is wrong)
完整代碼如下:
_username=‘huang‘_password=‘1234‘username=input("username:")password=input("password:")print(username,password)if _username==username and _password==password: print(‘Landfall...‘)else: print(‘The username or password you entered is wrong ‘)
程式啟動並執行結果:
看看是不是與啟動並執行結果一樣啊,如果是恭喜你學會了if and else的用法了。
python基礎學習(四)沒有寫完明天。。。。。