python 條件判斷 if 語句

2021-10-23 07:35:11 字數 683 閱讀 8418

猜拳遊戲:

import random

player = int(input(『請輸入 (0)石頭 (1)剪刀 (2)布:』))

#隨機生成 0,1,2 三個整數

computer = random.randint(0, 2)

print(computer)

#玩家贏 player:0,computer:1;player:1,computer:2;player:2,computer:0

if (player == 0 and computer == 1) or (player == 1 and computer == 2) or (player == 2 and computer == 0):

print(『你贏了!!!』)

elif player == computer:

print(『平局』)

else:

print(『你輸了!!!』)

三元表示式:

對於if…else這種語句,可以直接使用三元表示式來替換。

x = num1 if num1 > num2 else num2

條件判斷注意事項:

if語句後面需要是判斷條件,判斷條件必須是乙個布林值。如果不是布林值,會將這個值自動轉換成為布林值

python裡支援 x < a < y 這樣的寫法。在其他型別的語言裡,不支援此類寫法

Python 條件判斷語句

if elif else語句 語法 if 條件表示式 塊 elif 條件表示式 塊 elif 條件表示式 塊 elif 條件表示式 塊 else 塊 執行流程 if elif else語句在執行時,會自上向下依次對條件表示式進行求值判斷,如果表示式的結果為true,則執行當前 塊,然後語句結束 如果...

php if 判斷條件語句,PHP條件判斷語句

php 條件語句 在您編寫 時,經常會希望為不同的決定執行不同的動作。您可以在 中使用條件語句來實現這一點。在 php 中,我們可以使用以下條件語句 if 語句 如果指定條件為真,則執行 if else 語句 如果條件為 true,則執行 如果條件為 false,則執行另一端 if elseif e...

學習筆記 Python條件判斷 If語句

例項 usr bin env python3 coding utf 8 tuple的使用 if語句 print 請輸入數字 型別轉換,不然會報錯 age int input if age 18 print d d age,18 elif age 18 print d d age,18 else pr...