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

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

通過(guò)調(diào)用兩個(gè)函數(shù) Pygame 實(shí)時(shí)更新文本

通過(guò)調(diào)用兩個(gè)函數(shù) Pygame 實(shí)時(shí)更新文本

達(dá)令說(shuō) 2022-06-02 17:25:25
我有一個(gè)程序,它接受用戶的輸入并使用該P(yáng)opulation()函數(shù)顯示輸入的多種變體。該store_fit函數(shù)將這些不同的變體添加到列表中,然后將它們刪除,以便列表一次僅填充一個(gè)變體。我希望能夠從列表中獲取變體并使用它來(lái)更新我的文本。Population但是,我的程序僅在功能完成后才更新文本。我怎樣才能運(yùn)行該P(yáng)opulation功能并同時(shí)更新我的文本?代碼:fit = []...def store_fit(fittest): # fittest is each variation from Population    clear.fit()    fit.append(fittest)...pg.init()...done = Falsewhile not done:...    if event.key == pg.K_RETURN:        print(text)        target = text        Population(1000) #1000 variations        store_fit(value)        # I want this to run at the same time as Population        fittest = fit[0]...top_sentence = font.render(("test: " + fittest), 1, pg.Color('lightskyblue3'))screen.blit(top_sentence, (400, 400))
查看完整描述

1 回答

?
智慧大石

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

我建議制作Population一個(gè)生成器功能。請(qǐng)參閱Python yield 關(guān)鍵字解釋:


def Populate(text, c):

    for i in range(c):


        # compute variation

        # [...]


        yield variation

創(chuàng)建一個(gè)迭代器并用于next()檢索循環(huán)中的下一個(gè)變體,因此您可以打印每個(gè)變體:


populate_iter = Populate(text, 1000)


final_variation = None

while not done:


    next_variation = next(populate_iter, None)

    if next_variation :

        final_variation = next_variation 


        # print current variation

        # [...]


    else:

        done = True


根據(jù)評(píng)論編輯:


為了讓我的問(wèn)題簡(jiǎn)單,我沒(méi)有提到Population, 是一個(gè)類 [...]


當(dāng)然Populate can be a class,也是。在這種情況下,您必須實(shí)現(xiàn)該object.__iter__(self)方法。例如:


class Populate:

    def __init__(self, text, c):

        self.text = text

        self.c    = c


    def __iter__(self):

        for i in range(self.c):


            # compute variation

            # [...]


            yield variation

通過(guò)創(chuàng)建一個(gè)迭代器iter()。例如:


populate_iter = iter(Populate(text, 1000))


final_variation = None

while not done:


    next_variation = next(populate_iter, None)

    if next_variation :

        final_variation = next_variation 


        # print current variation

        # [...]


    else:

        done = True


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

添加回答

舉報(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)