# -*- coding: utf-8 -*-
# @Time : 2019/11/5 10:15
# @Author : John
# @Email : 2398344557@qq.com
# @File : 條件判斷語句.py
# @Software: PyCharm
Python中:
- 任何非0和非空None的值都為True
- 反之0或None則為False
- if語句的基本形式
# if 判斷條件:
# 執(zhí)行語句
# else:
# 執(zhí)行語句
result = 59
if result >= 60:
print('及格')
else:
print('不及格')
—— 不及格
# ----------------------------------------
# *** 拓展***
# 創(chuàng)建if語句快捷方式:條件后.if,然后按tab鍵
# result >= 60.if <== tab鍵
# ----------------------------------------
# *** 拓展***
# 這樣寫也成立:
if 1:
print('if執(zhí)行了')
if True:
print('if也執(zhí)行了')
if '1':
print('if執(zhí)行了')
if [0]:
print('if執(zhí)行了')
if {0}:
print('if執(zhí)行了')
if {'0': 0}:
print('if執(zhí)行了')
if (0,):
print('if執(zhí)行了')
...
—— if執(zhí)行了
# 結論:if條件為真時,就會執(zhí)行執(zhí)行語句
# ----------------------------------------
# *** 拓展***
# 三目運算符
#格式:print('' if 條件 else '')
print('及格' if result >=60 else '不及格')
—— 不及格
- if 語句多個判斷的形式
score = int(input('請輸入你的成績:'))
if score > 90:
print('你的成績?yōu)閮?yōu)秀')
elif score >80:
print('你的成績?yōu)榱己?)
elif score > 60:
print('你的成績?yōu)榧案?)
elif score < 60:
print('你的成績?yōu)椴患案?)
—— 請輸入你的成績:91
你的成績?yōu)閮?yōu)秀
請輸入你的成績:79
你的成績?yōu)榧案?
- if語句多個條件同時判斷的形式
# 很多編程語言使用switch去判斷多個條件
# Python中并沒有提供sswitch語句,而是使用elif來代替
# or(或),表示兩個條件有一個成立了時,判斷條件成立(一真必真)
# and(與),表示只有當兩個條件同時成立時,判斷條件成立(一假必假)
# 格式:
# if 情況1:
# 事情1
# elif 情況2:
# 事情2
# ...
# else:
# 以上都不滿足時執(zhí)行的事情
score = int(input('請輸入你的成績:'))
if score >= 90 and score <= 100:
print('你的成績?yōu)閮?yōu)秀')
elif score >= 75 and score < 90:
print('你的成績?yōu)榱己?)
elif score >= 60 and score < 75:
print('你的成績?yōu)榧案?)
elif score < 60:
print('你的成績?yōu)椴患案?)
else:
print('請輸入數(shù)字!100及以內(nèi)')
—— 請輸入你的成績:100
你的成績?yōu)閮?yōu)秀
請輸入你的成績:80
你的成績?yōu)榱己? 請輸入你的成績:70
你的成績?yōu)榧案? 請輸入你的成績:59
你的成績?yōu)椴患案?# 結論:1. elif必須與if一起使用
# 2. else可以選擇性使用,是以上情況(條件)都不滿足時使用
# ***拓展***
# ----------------
# 當if有多個條件時可以使用括號來區(qū)分判斷的先后循序,括號中的判斷優(yōu)先執(zhí)行(括號提高優(yōu)先級),此外,and和or 的優(yōu)先級低于>(大于),<(小于)等判斷符號,即大于和小于在沒有括號的 情況會比“與”、“或”要優(yōu)先判斷
java = 86
python = 68
if (80 <= java < 90) or (80 <= python < 90):
print('good')
—— good
- if的嵌套
# if 條件1:
# 滿足條件1時執(zhí)行的事情
# if 條件2:
# 滿足條件1執(zhí)行的事情
# 說明:
# 外層的if判斷,也可以是if else
# 內(nèi)層的if判斷,也可以是if else
【課堂練習】我想買車,買什么車決定于我在銀行有多少存款
# 要求:
# 如果我的存款超過500萬,我就買路虎
# 否則,如果我的存款超過100萬,我就買寶馬
# 否則, 如果我的存款超過50萬,我就買邁騰
# 否則, 如果我的存款超過10萬,我就買福特
# 否則, 如果我的存款10萬以下 ,我買比亞迪
money = int(input('我在銀行有多少存款(萬)?'))
if money > 500:
print('我的存款超過了500萬,買路虎!')
elif money > 100 and money <= 500:
print('我的存款超過了100萬,買寶馬!')
elif money > 10 and money <= 100:
print('我的存款超過了10萬,買福特')
elif money > 0 and money <= 10:
print('我的存款10萬以下 ,買比亞迪')
—— 我在銀行有多少存款(萬)?501
我的存款超過了500萬,買路虎!
【課堂練習】輸入小明的考試成績,顯示所獲獎勵
# 要求:
# 成績==100分,爸爸給他買輛車
# 成績>=90分,媽媽給他買MP4
# 90分>成績>=60分,媽媽給他買本參考書
# 成績<60分,什么都不買
grade = int(input('小明的考試成績?yōu)椋?))
if grade == 100:
print('爸爸給他買輛車')
elif grade >= 90 and grade < 100:
print('媽媽給他買MP4')
elif grade >= 60 and grade <90:
print('媽媽給他買本參考書')
elif grade >= 0 and grade < 60:
print('買買買,吔屎啦你')
—— 小明的考試成績?yōu)椋?9
買買買,吔屎啦你
【練習】不知為啥要帶小刀去見梁非凡???細思極恐..
chePiao = 1
# chePiao = 0
knife_length = 9
if chePiao == 1: # 1代表有車票,0代表沒車票
print('有車票了,可以上火車')
if knife_length < 10:
print('通過安檢')
print('終于可以見到梁非凡了,嘻嘻嘻~~~')
else:
print('沒通過安檢')
print('oh my god,刀子超過規(guī)定長度,等待警察處理')
else:
print('沒車票,不可以上火車')
print('嗚嗚嗚,只能下一次再見面了,一票難求吖!')
—— 有車票了,可以上火車
通過安檢
終于可以見到梁非凡了,嘻嘻嘻~~~
(其他情況略)
【練習】請輸入飛船系統(tǒng)啟動的語音命令(雷神3中)
voice = input('Welcome. Voice activation required:')
if voice == 'Banner':
print('Welcome, strongest Avenger.')
elif voice == 'Point Break':
print('Welcome, Point Break.')
else:
print('Access denied')
—— Welcome. Voice activation required:Thor
Access denied
Welcome. Voice activation required:Thor, son of Odin.
Access denied
Welcome. Voice activation required:God of Thunder
Access denied
Welcome. Voice activation required:Strongest Avenger
Access denied
Welcome. Voice activation required:Point Break
Welcome, Point Break.
Welcome. Voice activation required:Banner
Welcome, strongest Avenger.

Voice activation required.png
【練習】人機猜拳游戲(詳細趣味版)
# 要求:
# 鍵盤輸入,剪刀(0),石頭(1),布(2)
# 電腦隨機生成一個數(shù),范圍為[0, 2]
# 判斷玩家和電腦猜拳輸贏的情況,并且給予顯示
from random import randint
operation = 'yes'
while operation == 'yes':
my_finger = input('聽說挺厲害的,我要跟你單挑猜拳,請輸入你的出拳(0為剪刀,1為石頭,布為2):')
if my_finger in ['0', '1', '2']:
my_finger = int(my_finger)
computer_finger = randint(0, 2)
print('********數(shù)據(jù)正在處理中********')
my_finger_ChineseDisplay = ''
computer_finger_ChineseDisplay = ''
if my_finger == 0:
my_finger_ChineseDisplay = '剪刀'
elif my_finger == 1:
my_finger_ChineseDisplay = '石頭'
elif my_finger == 2:
my_finger_ChineseDisplay = '布'
if computer_finger == 0:
computer_finger_ChineseDisplay = '剪刀'
elif computer_finger == 1:
computer_finger_ChineseDisplay = '石頭'
elif computer_finger == 2:
computer_finger_ChineseDisplay = '布'
print('你的出拳為{}-{}'.format(my_finger, my_finger_ChineseDisplay))
print('我的出拳為{}-{}'.format(computer_finger, computer_finger_ChineseDisplay))
if my_finger == computer_finger:
print('平局!小伙子,看來你挺厲害的!')
print('可我不服,再來!')
operation = 'yes'
print('--------------------------------------')
elif my_finger > computer_finger:
if (my_finger - computer_finger) == 1:
print('呵呵,你竟然贏了!')
print('給我等著,我一定會回來的!')
operation = 'no'
break
else:
print('哦豁,你輸了!老弟,你是怎么回事?')
print('要不再來一局?')
operation = 'yes'
print('--------------------------------------')
elif my_finger < computer_finger:
if (computer_finger - my_finger) == 1:
print('哦豁,你輸了!老弟,你是怎么回事?')
print('要不再來一局?')
operation = 'yes'
print('--------------------------------------')
else:
print('呵呵,你竟然贏了!')
print('給我等著,我一定會回來的!')
operation = 'no'
break
else:
print('請輸入0或1或2!')
詳細趣味版猜拳效果如下:
【練習】人機猜拳游戲(簡短版)
from random import randint
while True:
player = input('剪刀[0],石頭[1],布[2]:')
computer = randint(0, 2)
if (player == 0 and computer == 2)or(player == 1 and computer == 0)or(player == 2 and computer == 1):
print('這是高手,你贏了。')
break
elif player == computer:
print('哈哈,平局呃,要不再來一局?')
else:
print('你輸了,不要走,洗洗手,決戰(zhàn)到天亮。')
簡短版猜拳效果如下:
剪刀[0],石頭[1],布[2]:0
哈哈,平局呃,要不再來一局?
剪刀[0],石頭[1],布[2]:0
哈哈,平局呃,要不再來一局?
剪刀[0],石頭[1],布[2]:0
你輸了,不要走,洗洗手,決戰(zhàn)到天亮。
剪刀[0],石頭[1],布[2]:0
這是高手,你贏了。