【问题标题】:The random choice for a word from a list doesn't work properly (Python/Tkinter) [closed]列表中单词的随机选择无法正常工作(Python/Tkinter)[关闭]
【发布时间】:2016-05-09 01:22:47
【问题描述】:

您好,我正在研究颜色 stroop,我可以用我的代码更改单词和单词的颜色,但是当我的函数“next_selected”被调用时,我不能只更改单词,可以有人帮帮我吗?

def tick():
    global doTick
    global sec
    if not doTick:
        return
    sec += 0.1
    sec = round(sec, 1)
    ftest1.after(100, tick)
    time2Label.configure(text=sec)
    if sec == 60.0:
        doTick = False
        time2Label.config(text=sec)
        label1.config(text=score, fg='black')

def start():
    global doTick
    doTick = True
    label1.pack()
    tick()
    startbutton1.destroy()

COLORS = ['blue','green','yellow','red']

def stimulus(same):

    global word
    colors = list(COLORS)

    if same:
        return (word)

    colors.remove(word)

    return (word)

def next_selected():
    global word
    word = stimulus(choice((True,False)))

    label1.config(text=word)
    label1.update()

【问题讨论】:

  • 为什么不能改?当你尝试时会发生什么?
  • 没什么,这个词没有出现。控制台知道它是否是正确的单词,但是出现下一个随机单词的功能不起作用(对不起我的英语)
  • 我只是不知道怎么做,我是 Python 新手
  • 你认为刺激措施会做什么
  • 我在这里看到的都是定义,你能说明你如何调用你的函数,你得到什么,你想得到什么?

标签: python random tkinter


【解决方案1】:

stimulus()same=False 时无法选择新颜色,也无法将新选择保存在全局word 中。试试这个:

COLORS = ['blue', 'green', 'yellow', 'red']

word = COLORS[0]

def stimulus(same):

    global word

    if same:  # return the previous color
        return (word)

    colors = list(COLORS)

    colors.remove(word)

    word = choice(colors)  # chose a different color & remember it

    return word

def next_selected():
    global word
    word = stimulus(choice((True, False)))

    label1.config(text=word)
    label1.update()

【讨论】:

  • 非常感谢这一切都很好!我的颜色 stroop 测试工作知道:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多