如何在while循环中停留,但暂停接收新按钮inpu

2024-09-30 10:29:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我是编程新手,我似乎不知道如何让“konto”不断更新,而不是在一段时间内循环。while循环的问题是它不允许输入新按钮,因为它会冻结窗口。你知道吗

我试过让“konto”成为本地的,把代码分成不同的函数,改变循环样式。Break退出循环,这样“konto”就不会更新。你知道吗

konto = 100
roulette_window = Tk()

def roulette(chosen_color, sats, konto):
    while True:
        x = randint(0, 36)
        if x == 0:
            green_num = x
            print(green_num, 'Green')
            color = ('Green')
            if color and chosen_color == 'Green':
                win_amount = 35 * int(sats)
        elif (x % 2) == 0:
            red_num = x
            print(red_num, 'Red')
            color = ('Red')
            if color and chosen_color == 'Red':
                win_amount = 2 * int(sats)
        elif (x % 2) == 1:
            black_num = x
            color = ('Black')
            print(black_num, 'Black')
            if color and chosen_color == 'Black':
                win_amount = 2 * int(sats)
        if not color == chosen_color:
            win_amount = 0
        konto = konto - int(sats) + int(win_amount)
        print(konto)

def bet_black():
    sats = bet_input.get(1.0, END)
    chosen_color = 'Black'
    bet_input.delete(1.0, END)
    roulette(chosen_color, sats, konto)


def bet_red():

我想能够调用函数“轮盘赌”一次,例如赌黑按钮点击,然后点击赌红后一个循环,而仍然有更新的“konto”变量。你知道吗


Tags: ifdefgreenamountwinnumcolorint
1条回答
网友
1楼 · 发布于 2024-09-30 10:29:25

首先,您的变量konto是一个全局变量,您需要向python表明它是全局变量。将方法更改为:

def roulette(chosen_color, sats):
    global konto
    ...

Python中的变量具有作用域规则,这些规则决定了它们的有效位置。任何时候,只要将变量指定给某个变量,就会创建该变量的本地版本,除非已将其显式标记为全局变量。你知道吗

其次,我不知道为什么这里有while True结构。这将导致一个无限循环,除非您break退出它或return。你知道吗

相关问题 更多 >

    热门问题