相關(guān)推薦
您可能對(duì)下面課程感興趣
Python上位機(jī)開(kāi)發(fā)教程

105小節(jié)已有8556人學(xué)過(guò)

Python上位機(jī)開(kāi)發(fā)多分支條件語(yǔ)句
發(fā)布時(shí)間:2024-11-20 09:29 [ 我要自學(xué)網(wǎng)原創(chuàng) ] 發(fā)布人: 快樂(lè)小女 閱讀: 109

'''
多分支條件語(yǔ)句的基本結(jié)構(gòu)是 if-elif-else 。

if 條件1:
    # 當(dāng)條件1 為真時(shí)執(zhí)行的代碼
elif 條件2:
    # 當(dāng) 條件1 為假,條件2 為真時(shí)執(zhí)行的代碼
elif 條件3:
    # 當(dāng) 條件1 和 條件2 都為假,條件3 為真時(shí)執(zhí)行的代碼
...
else:
    # 當(dāng)所有條件都為假時(shí)執(zhí)行的代碼

強(qiáng)調(diào)要點(diǎn)
條件的順序很重要,先檢查更嚴(yán)格或更特殊的條件。
只有一個(gè)分支的代碼會(huì)被執(zhí)行,一旦某個(gè)條件滿(mǎn)足,后續(xù)的條件不再檢查。
else 是可選的,但在某些情況下可以處理所有前面條件都不滿(mǎn)足的情況,使邏輯更完整。

#python3.10 之后提供了match - case 語(yǔ)句

match expression:
    case pattern1:
        # 執(zhí)行的代碼塊 1
    case pattern2:
        # 執(zhí)行的代碼塊 2
   ...
    case _:
        # 當(dāng)所有前面的模式都不匹配時(shí)執(zhí)行的默認(rèn)代碼塊


'''

score = 40

# if score >= 90:
#     print("優(yōu)秀")
# elif score >= 80:
#     print("良好")
# elif score >= 70:
#     print("中等")
# elif score >= 60:
#     print("及格")
# else:
#     print("不及格")

# match score:
#     case score if score>=90:
#         print("優(yōu)秀")
#     case score if score>=80:
#         print("良好")
#     case score if score>=70:
#         print("中等")
#     case score if score>=60:
#         print("及格")
#     case _:
#         print("不及格")









from PySide6.QtWidgets import *
from PySide6.QtCore import *

class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.setWindowTitle("多條件分支示例")
        self.resize(500,300)

        # 創(chuàng)建按鈕
        self.button1 = QPushButton("按鈕 1",self)
        self.button2 = QPushButton("按鈕 2",self)
        self.button3 = QPushButton("按鈕 3",self)

        # 擺放位置
        self.button1.setGeometry(40,40,80,30)
        self.button2.setGeometry(40,80,80,30)
        self.button3.setGeometry(40,120,80,30)



        # 連接按鈕的點(diǎn)擊信號(hào)到槽函數(shù)
        self.button1.clicked.connect(self.on_button_clicked)
        self.button2.clicked.connect(self.on_button_clicked)
        self.button3.clicked.connect(self.on_button_clicked)

    def on_button_clicked(self):
        # 根據(jù)發(fā)送信號(hào)的對(duì)象(按鈕)執(zhí)行不同的操作,sender() 方法獲取到發(fā)送這個(gè)信號(hào)的對(duì)象
        sender = self.sender()
        # if sender == self.button1:
        #     QMessageBox.information(self, "消息", "您點(diǎn)擊了按鈕 1")
        # elif sender == self.button2:
        #     QMessageBox.information(self, "消息", "您點(diǎn)擊了按鈕 2")
        # elif sender == self.button3:
        #     QMessageBox.information(self, "消息", "您點(diǎn)擊了按鈕 3")

        #第二種寫(xiě)法
        match sender:
            case self.button1:
                QMessageBox.information(self, "消息", "您點(diǎn)擊了按鈕 1")
            case self.button2:
                QMessageBox.information(self, "消息", "您點(diǎn)擊了按鈕 2")
            case self.button3:
                QMessageBox.information(self, "消息", "您點(diǎn)擊了按鈕 3")

app = QApplication([])
window = MainWindow()
window.show()
app.exec()

Python上位機(jī)開(kāi)發(fā)教程
我要自學(xué)網(wǎng)商城 ¥80 元
進(jìn)入購(gòu)買(mǎi)
文章評(píng)論
0 條評(píng)論 按熱度排序 按時(shí)間排序 /350
添加表情
遵守中華人民共和國(guó)的各項(xiàng)道德法規(guī),
承擔(dān)因您的行為而導(dǎo)致的法律責(zé)任,
本站有權(quán)保留或刪除有爭(zhēng)議評(píng)論。
參與本評(píng)論即表明您已經(jīng)閱讀并接受
上述條款。
V
特惠充值
聯(lián)系客服
APP下載
官方微信
返回頂部
相關(guān)推薦
您可能對(duì)下面課程感興趣
Python上位機(jī)開(kāi)發(fā)教程

105小節(jié)已有8556人學(xué)過(guò)

分類(lèi)選擇:
電腦辦公 平面設(shè)計(jì) 室內(nèi)設(shè)計(jì) 室外設(shè)計(jì) 機(jī)械設(shè)計(jì) 工業(yè)自動(dòng)化 影視動(dòng)畫(huà) 程序開(kāi)發(fā) 網(wǎng)頁(yè)設(shè)計(jì) 會(huì)計(jì)課程 興趣成長(zhǎng) AIGC