python語法(二)
阿新 • • 發佈:2018-12-18
昨天簡單的學習了一些python的一些簡單的語句與python的資料型別,今天繼續學習python的基礎語句 if 語句。
一、if 語句
- if 語句語法
if expression:
ifSuite
else:
elseSuite
如果表示式expression的值為非0或者為True,則程式碼組ifSuite將會被執行,否則執行elseSuite程式碼組。其實這裡跟java的語法差不多,但是這裡expression可以直接寫資料型別。
- 數字,只要是非0,則會被認為是True。
- 字串 只要是非“”, 都會被認為是True。
- 列表 只要非[], 都會被認為是True。
- 元組 只要非(), 都會被認為是True。
- 字典 只要非{}, 都會被認為是True。
- None 也為假
# -*- coding: utf-8 -*- ''' Created on 2018年12月17日 @author: Herrt灬凌夜 ''' if 0.1 : print("真"); if 0 : print("真"); else : print("假"); if "" : print("真"); else : print("假"); if " ": print("真"); else : print("假"); if [] : print("真"); else : print("假"); if () : print("真"); else : print("假"); if {} : print("真"); else : print("假"); if None : print("真"); else : print("假"); if "s" in "sdas" : print("真");
以下為上述if語句的執行結果:
最後寫一個簡單的例子:
1.提示使用者輸入使用者名稱密碼
2.若使用者名稱為"tom",密碼為123456則返回Login否則返回error
# -*- coding: utf-8 -*- ''' Created on 2018年12月17日 1.提示使用者輸入使用者名稱密碼 2.若使用者名稱為"tom",密碼為123456則返回Login否則返回error @author: Herrt灬凌夜 ''' userName = input("請輸入使用者名稱:"); passWord = input("請輸入密碼:"); if userName == "tom" and passWord == "123456": print("Login"); else : print("error");
二、if elif 語句
- if elif 語句語法
if expression1 : ifSuite elif expression2 : elifSuite else : elseSuite
if elif 語句就相當於java語言中if else 語句一樣,主要是用於多條件判斷語句。我們以一個簡單的例項來看這個語句。
1.輸入一個數字,如果這個數字大於0,輸出正數,如果小於0,輸出負數,如果等於0則輸出0
# -*- coding: utf-8 -*- ''' Created on 2018年12月17日 輸入一個數字,如果這個數字大於0,輸出正數,如果小於0,輸出負數,如果等於0則輸出0 @author: Herrt灬凌夜 ''' numStr = int(input("請輸入一個數字:")); if numStr > 0 : print("正數"); elif numStr < 0 : print("負數"); else : print("0");
三、一些簡單的例子
- 根據使用者輸入的成績,輸出,如果大於60則輸出“及格”,如果大於70則輸出“良好”,如果大於80則輸出“好”, 如果大於90則輸出優秀,否則輸出“不及格”
# -*- coding: utf-8 -*- ''' Created on 2018年12月17日 根據使用者輸入的成績,輸出,如果大於60則輸出“及格”,如果大於70則輸出“良好”,如果大於80則輸出“好”, 如果大於90則輸出優秀,否則輸出“不及格” @author: Herrt灬凌夜 ''' grade = int(input("請輸入成績")); if grade < 0 or grade > 100 : print("輸入成績有誤!"); elif grade > 60 and grade <= 70 : print("及格"); elif grade > 70 and grade <= 80 : print("良好"); elif grade > 80 and grade <= 90 : print("好"); elif grade > 90 : print("優秀"); else : print("不及格");
- 寫一個人機互動的猜拳遊戲
# -*- coding: utf-8 -*- ''' Created on 2018年12月17日 猜拳遊戲 @author: Herrt灬凌夜 ''' import random; computer = random.choice(["石頭", "剪刀", "布"]); person = input("請出拳(石頭,剪刀,布):"); if person == "石頭" and computer == "石頭" : print("平局"); elif person == "剪刀" and computer == "剪刀" : print("平局"); elif person == "布" and computer == "布" : print("平局"); elif person == "石頭" and computer == "剪刀" : print("你贏了"); elif person == "剪刀" and computer == "布" : print("你贏了"); elif person == "布" and computer == "石頭" : print("你贏了"); elif person == "石頭" and computer == "剪刀" : print("你輸了"); elif person == "剪刀" and computer == "布" : print("你輸了"); elif person == "布" and computer == "石頭" : print("你輸了");
上述程式碼雖然可以實現猜拳遊戲,但是顯得非常的囉嗦,我們對上述程式碼進行優化,並且可以讓使用者有更好的體驗。
# -*- coding: utf-8 -*- ''' Created on 2018年12月17日 猜拳遊戲 @author: Herrt灬凌夜 ''' import random; computer = random.choice(["石頭", "剪刀", "布"]); winList = [["石頭", "剪刀"], ["剪刀", "布"], ["布", "石頭"]]; personList = ["石頭", "剪刀", "布"]; hint = """ 請出拳: (0)石頭 (1)剪刀 (3)布 """; #獲取下標 person = int(input(hint)); #字串輸出格式化 print("你出了%s,電腦出了%s" % (personList[person], computer)); if personList[person] == computer : #\033[32;1m 開啟顏色 \033[0m 關閉顏色 print("\033[32;1m平局\033[0m"); elif [personList[person], computer] in winList : print("\033[31;1m你贏了\033[0m"); else : print("\033[31;1m你輸了\033[0m");
下面的程式碼明顯就簡潔了許多,而且可讀性也強了許多。
今天主要學習了python的if 判斷語句,在開發過程中判斷語句還是比較常用的語句,所以要加強練習,畢竟學習一門新的語言,練習是必不可少的。
-------------------- END ---------------------
最後附上作者的微信公眾號地址和部落格地址
公眾號:wuyouxin_gzh
Herrt灬凌夜:https://www.cnblogs.com/wuyx/