第五章:條件控制語句

1.if語句

語法 : if條件表達式

代碼塊

執行的流程 : if語句在執行時,會先對條件表達式進行求值判斷,如果爲True,則執行if後面的語句
如果爲False,則不執行

if True : print('hello')
>>>hello
if False : print('hello')
>>>不執行,不打印結果

num = 50
if num > 20 : print('num比20大')
>>>num 比20大

num = 10
if num > 20 : print('num比20大')
>>>不執行,不打印結果

if 條件表達式
代碼塊

什麼是代碼塊?
代碼塊當中保存着一組代碼,同一個代碼塊中的代碼,要麼都執行要麼都不執行
代碼塊就是一種爲代碼分組的機制

if True:
    print('haha')
    print('abc')
    print('heihei')
    >>>haha
    abc 
    heihei

if False        :
    print('haha')
    print('abc')
    print('heihei')
>>>不執行,不打印結果
num = 30
if num > 20 and num < 40:
    print('num比20大,比40小')
>>>num比20大,比40小

num = 30
if 20 < num < 40:
    print('num比20大,比40小')
>>>num比20大比40小

2.input函數

input函數接受一個標準輸入數據,返回爲 string 類型。
input()也可以用於阻止程序結束。

 需求:獲取用戶輸入的最喜歡玩的遊戲的名稱遊戲名稱是王者榮耀——>歡迎來到王者榮耀

username = input('請輸入您最喜歡玩的遊戲的名稱:')
if username == '王者榮耀':
    print('歡迎來到王者榮耀')
>>>請輸入您最喜歡玩的遊戲名稱:王者榮耀
歡迎來到王者榮耀

3.if-else語句

if-else語句
語法:
if 條件表達式:
代碼塊
else:
代碼塊
執行流程:
if-else語句在執行是,先對if後面的條件表達式進行求值判斷
如果爲True,則執行if後面的代碼塊
如果爲False,則執行else後面的代碼塊

value = input('請輸入你的工資:')

value = int(value)

# 如果用戶的工資大於2000 就執行一個打印語句
if value >= 2000:
    print('你可以養活自己了')
else:
    print('你該加油了')

4.if-elif-else語句

執行流程:
if-elif-else語句在執行時,會自上向下依次對條件表達式進行求值判斷,
如果表達式的結果爲True,則執行當前代碼塊,然後語句結束
如果表達式的結果爲False,則繼續向下判斷,直到找到True爲止
如果所有表達式都是False,則執行else後的代碼塊
if-elif-else中只會有一個代碼塊會執行

語法:
if-條件表達式:
代碼塊
elif 條件表達式:
代碼塊
elif 條件表達式:
代碼塊
elif 條件表達式:
代碼塊
else:
代碼塊

value = 999

if value >=30000:
    print('有錢就是任性')

elif value >= 20000:
    print('有錢真好')

elif value >= 10000:
    print('哥也是月薪上萬')

elif value >= 5000:
    print('工資還說得過去')

else:
    print('你該加油了')
 >>>你該加油了
value = 6000

if value >=2000 and value < 5000:
    print('能養活自己了')

elif value >= 5000 and value < 10000:
    print('工資還說得過去')

elif value >= 10000:
    print('哥也是月薪上萬')
 >>>工資還說的過去

5.if語句練習

練習1 根據指定月份所屬的季節

3 4 5 春季

6 7 8 夏季

9 10 11 秋季

#12 1 2 冬季

month = 7

if month == 3 or month == 4 or month == 5:
    print(month,'春季')

elif month == 6 or month == 7 or month == 8:
    print(month,'夏季')

elif month == 9 or month == 10 or month == 11:
    print(month,'秋季')

elif month == 12 or month == 1 or month == 2:
    print(month,'冬季')
    >>>7 夏季

month = 12

if month > 12 or month < 1:
    print('月份不存在')

elif 3 <= month <= 5:
    print(month,'春季')

elif 6 <= month <= 8:
    print(month,'夏季')

elif 9 <= month <= 11:
    print(month,'秋季')

else:
    print(month,'冬季')
>>>冬季

練習2 編寫一個程序,獲取用戶輸入的一個整數,然後顯示這個數奇數還是偶數

獲取用戶輸入的整數
num = int(input('請輸入一個任意的整數:'))

# 顯示num是奇數還是偶數
if num % 2 == 0:
    print(num,'是偶數')
else:
    print(num,'是奇數')

練習3 編寫一個程序,檢查任意一個年份是否是閏年

一般的,能被4整除的月份是閏年,並且不能被100整除,不能被4整除的年份是平年

或者可以被400整除的也是閏年

二月有29天就是閏年 有28天就是平年

year = int(input('請輸入任意一個年份:'))

if year % 4 == 0 and year % 100 != 0 or year % 400 ==0:
    print(year,'是閏年')
else:
    print(year,'是平年')

練習4 編寫一個程序,狗和人的年齡, 輸入狗的年齡來顯示 人的年齡

1 .如果輸入的是負數,請提示一個顯示信息
2.狗的年齡是5歲,那麼相當於人的年齡是多大呢?
3.換算關係,狗的前兩年,每一年相當於人的10.5歲,然後每增加一年就增加四歲
4. 5歲的狗相當於人類的年齡是 10.5 + 10.5 + 4 + 4 + 4 = 33

方案一:



# 方案一:
dog_age = float(input('請輸入狗的年齡:'))

person_age = 0
#如果狗的年齡在兩歲以下的情況(包含兩歲)
if dog_age > 0:

    if dog_age <= 2:
        person_age = dog_age * 10.5
    # 狗的年齡在兩歲以上
    else:
        #計算狗在兩歲時相當於人的年齡的多少歲
        person_age = 2 * 10.5
        #計算狗超過兩後的年齡
        person_age += (dog_age - 2) * 4
    print(dog_age , '歲的狗,年齡相當於' , person_age , '歲的人')
else:
    print('您輸入的年齡不合法')

方案二:

# 方案二:(個人方案)
dog_age = float(input('請輸入狗的年齡:'))

if dog_age < 0:
    print('對不起,您輸入的年齡不存在')

elif 0 < dog_age <= 2:
    person_age = dog_age * 10.5
    print(dog_age, '歲的狗', '相當於', person_age, '歲的人')
else:
    person_age = 2 * 10.5
    person_age += (dog_age - 2) * 4
    print(dog_age , '歲的狗' , '相當於' , person_age , '歲的人')

練習5 編寫一個程序,獲取小紅的成績

當成績爲100分 獎勵一臺電腦 (滿分100)
當成績爲80-99 獎勵一部手機
當成績爲60-79 獎勵一本書
其他什麼都不獎勵,一邊玩兒去


# 獲取小紅的成績
score = float(input('請輸入小紅的成績(0-100):'))

if 0 <= score <=100:
    # 根據分數判斷獎勵什麼東西
    if score == 100:
        print('獎勵一臺電腦')

    elif score >= 80:
        print('獎勵一部手機')

    elif score >= 60:
        print('獎勵一本書')
    else:
        print('一邊玩兒去')
else:
    print('對不起,您輸入的成績不標準')

6.while語句

循環語句 可以指定代碼塊重置指定次數
分爲兩種:while循環和 for循環
while循環
語法:
while 條件表達式:
代碼塊
else:
代碼塊

執行流程:
while語句在執行時,會先對while後的條件表達式進行求值判斷,
如果判斷結果爲True,則執行循環體(代碼塊),
循環體執行完畢,繼續對條件表達式進行求值判斷,以此類推,
直到判斷結果爲False,則循環終止,


# 循環的三個條件

# 第一個條件:初始化表達式  定義一個變量
i = 0
while i < 20: # 第二個條件就是:條件表達式 用來設置執行循環的條件

    print(i)

    # 第三個條件:更新表達式
    i += 1  #(相當於 i = i + 1)

i = 0
while i < 11:

    i += 1

    print(i)
>>>1
2
3
4
5
6
7
8
9
10
11

i = 1
while i < 11:

    print(i)

    i += 1
   
>>>0
1
2
3
4
5
6
7
8
9
10

i = 0
while i < 10:
    i += 1
    print(i)
else:
    print('執行了else語句')
 >>>1
2
3
4
5
6
7
8
9
10
執行了else語句

7.while語句練習

練習1 求100以內所有的偶數之和


# 方案一: 先獲取100以內所有的數
i = 0
# 定義一個變量保存結果
result = 0
while i < 100:
    i += 1

    #判斷 i 是否是偶數
    if i % 2 == 0:

        result += i  #(result = result + i)
    print('結果爲',result)
 >>>結果爲 0
結果爲 2
結果爲 2
結果爲 6
結果爲 6
結果爲 12
結果爲 12
結果爲 20
結果爲 20
結果爲 30
結果爲 30
結果爲 42
結果爲 42
結果爲 56
結果爲 56
結果爲 72
結果爲 72
結果爲 90
結果爲 90
結果爲 110
結果爲 110
結果爲 132
結果爲 132
結果爲 156
結果爲 156
結果爲 182
結果爲 182
結果爲 210
結果爲 210
結果爲 240
結果爲 240
結果爲 272
結果爲 272
結果爲 306
結果爲 306
結果爲 342
結果爲 342
結果爲 380
結果爲 380
結果爲 420
結果爲 420
結果爲 462
結果爲 462
結果爲 506
結果爲 506
結果爲 552
結果爲 552
結果爲 600
結果爲 600
結果爲 650
結果爲 650
結果爲 702
結果爲 702
結果爲 756
結果爲 756
結果爲 812
結果爲 812
結果爲 870
結果爲 870
結果爲 930
結果爲 930
結果爲 992
結果爲 992
結果爲 1056
結果爲 1056
結果爲 1122
結果爲 1122
結果爲 1190
結果爲 1190
結果爲 1260
結果爲 1260
結果爲 1332
結果爲 1332
結果爲 1406
結果爲 1406
結果爲 1482
結果爲 1482
結果爲 1560
結果爲 1560
結果爲 1640
結果爲 1640
結果爲 1722
結果爲 1722
結果爲 1806
結果爲 1806
結果爲 1892
結果爲 1892
結果爲 1980
結果爲 1980
結果爲 2070
結果爲 2070
結果爲 2162
結果爲 2162
結果爲 2256
結果爲 2256
結果爲 2352
結果爲 2352
結果爲 2450
結果爲 2450
結果爲 2550

# 方案二:先獲取100以內所有的偶數
i = 0
result = 0
while i < 100:
    i += 2
    # 把這些偶數相加
    result += i
print(result)
>>>2550
i = 0
while i < 11:
    i += 1
    print(i)
 >>>1
2
3
4
5
6
7
8
9
10
11

i = 0
while i < 11:
    i += 1
print(i)
>>>11

練習2 求出100以內所有9的倍數,以及個數 11

# 定義一個標記  目的記錄循環的次數
j = 0
i = 9
# 定義一個變量,接受9的倍數之和
sum = 0
while i < 100:

    j += 1
    sum += i
    i += 9

print('總和爲:',sum,'個數爲:',j)
>>>總和爲: 594 個數爲: 11

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章