python學習 02 條件語句

2021-10-08 11:17:46 字數 1989 閱讀 8340

if expression:

expr_true_suite

if

2>

1and

not2

>3:

print

('correct judgement!'

)# correct judgement!

if expression:

expr_true_suite

else

: expr_false_suite

temp = input("猜一猜小姐姐想的是哪個數字?")

guess = int(temp) # input 函式將接收的任何資料型別都預設為 str。

if guess == 666:

print("你太了解小姐姐的心思了!")

print("哼,猜對也沒有獎勵!")

else:

print("猜錯了,小姐姐現在心裡想的是666!")

print("遊戲結束,不玩兒啦!")

if語句支援巢狀,即在乙個if語句中嵌入另乙個if語句,從而構成不同層次的選擇結構。python 使用縮排而不是大括號來標記**塊邊界,因此要特別注意else的懸掛問題。

【例子】

hi =

6if hi >2:

if hi >7:

print

('好棒!好棒!'

)else

:print

('切~'

)

【例子】

temp =

input

("不妨猜一下小哥哥現在心裡想的是那個數字:"

)guess =

int(temp)

if guess >8:

print

("大了,大了"

)else

:if guess ==8:

print

("你這麼懂小哥哥的心思嗎?"

)print

("哼,猜對也沒有獎勵!"

)else

:print

("小了,小了"

)print

("遊戲結束,不玩兒啦!"

)

if expression1:

expr1_true_suite

elif expression2:

expr2_true_suite

..elif expressionn:

exprn_true_suite

else

: expr_false_suite

temp =

input

('請輸入成績:'

)source =

int(temp)

if100

>= source >=90:

print

('a'

)elif

90> source >=80:

print

('b'

)elif

80> source >=60:

print

('c'

)elif

60> source >=0:

print

('d'

)else

:print

('輸入錯誤!'

)

my_list =

['lsgogroup'

]my_list.pop(0)

assert

len(my_list)

>

0# assertionerror

assert

3>

7# assertionerror

02 條件語句

if 條件 條件成立後的 else 條件不成立後的 示例1print 開始 if 5 5 print 123 else print 456 print 結束 示例2num 19 if num 10 print num變數對應值大於10 else print num變數對應值不大於10 示例3 use...

Python基礎02 條件語句,迴圈語句

while else迴圈 當while迴圈正常執行完的情況下,執行else輸出,如果while迴圈中執行了跳出迴圈的語句,比如 break,將不執行else 塊的內容。for else迴圈 當for迴圈正常執行完的情況下,執行else輸出,如果for迴圈中執行了跳出迴圈的語句,比如 break,將不...

Python 02條件語句 迴圈語句

學習參考文件 一 條件語句 1.if語句 if 1 print 2.if else語句 a int input please input an interger if a 80 print a else print b 3.if elif else if a 80 print a elif a 70...