第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在 Python 中暫停和恢復(fù) while 循環(huán)?

如何在 Python 中暫停和恢復(fù) while 循環(huán)?

慕桂英3389331 2023-06-06 15:09:40
我想運(yùn)行一個(gè)循環(huán)來(lái)打印“Hello”,當(dāng)我按“K”時(shí)它停止打印但它不會(huì)結(jié)束程序,然后當(dāng)我再次按“K”時(shí)它會(huì)再次開(kāi)始打印。我試過(guò)這個(gè)(使用鍵盤(pán)模塊):import keyboardrunning = Truewhile running == True:    print("hello")    if keyboard.is_pressed("k"):        if running == True:            running = False        else:            running = True但是當(dāng)我按下按鈕時(shí),它只是結(jié)束了程序,而這不是我想要做的。我明白它為什么會(huì)結(jié)束,但我不知道如何讓它不結(jié)束。我怎樣才能做到這一點(diǎn)?
查看完整描述

3 回答

?
開(kāi)滿天機(jī)

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個(gè)贊

import keyboard


running = True

display = True

block = False


while running:

    if keyboard.is_pressed("k"):

        if block == False:

            display = not display

            block = True

    else:

        block = False

    if display:

        print("hello")

    else:

        print("not")


查看完整回答
反對(duì) 回復(fù) 2023-06-06
?
萬(wàn)千封印

TA貢獻(xiàn)1891條經(jīng)驗(yàn) 獲得超3個(gè)贊

也許是這樣的:


import keyboard


running = True

stop = False


while !stop:


    if keyboard.is_pressed("k"):

        running = !running          # Stops "hello" while

    if keyboard.is_pressed("q"):

        stop = !stop                # Stops general while


    if running:


        print("hello")


查看完整回答
反對(duì) 回復(fù) 2023-06-06
?
白衣染霜花

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超10個(gè)贊

您可以為按鍵使用一個(gè)處理程序,它設(shè)置一個(gè)事件,主線程可以定期測(cè)試該事件,并在需要時(shí)等待。


(請(qǐng)注意,這里有兩種類型的事件,按鍵事件和 的設(shè)置running,因此不要將它們混淆。)


from threading import Event

from time import sleep

import keyboard


hotkey = 'k'


running = Event()

running.set()  # at the start, it is running


def handle_key_event(event):

    if event.event_type == 'down':

        # toggle value of 'running'

        if running.is_set():

            running.clear()

        else:

            running.set()


# make it so that handle_key_event is called when k is pressed; this will 

# be in a separate thread from the main execution

keyboard.hook_key(hotkey, handle_key_event)


while True:

    if not running.is_set():

        running.wait()  # wait until running is set

    sleep(0.1)        

    print('hello')


查看完整回答
反對(duì) 回復(fù) 2023-06-06
  • 3 回答
  • 0 關(guān)注
  • 262 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)