这个顺序的逻辑有什么问题?

What is wrong with the logic in this sequence?

我是一个 Python 初学者,我决定在我的暑假期间创建一个程序,但在我这样做之前我决定先做一个小程序,练习创建 GUI。我正在开发一个笑话机,它使用随机模块 select 一个随机整数来决定显示哪个笑话,但是它似乎只输出笑话 3,无论我 运行它。

我在这里看不到任何问题,有什么建议吗?

joke_select = random.randint (1,3)

joke1 = "Why do you never see elephants hiding in trees?\n because they're really good at it!"
joke2 = "What is grey and can't climb a tree? \n A parking lot"
joke3 = "What is red and bad for your teeth?\n A brick"

if joke_select == '1':
    joke_label = tkinter.Label (main, text = joke1)
elif joke_select == '2':
    joke_label = tkinter.Label (main, text = joke2)
else:
    joke_label = tkinter.Label (main, text = joke3)

def get_joke():
    joke_label.pack ()
if joke_select == '1':

joke_select 只是一个整数,但您在这里将它与字符串进行比较。尝试与整数进行比较。

if joke_select == 1:

elif joke_select == '2':.

也是如此

您的脚本查找一个字符串并生成一个数字。

if joke_select == 1:
    joke_label = tkinter.Label (main, text = joke1)
elif joke_select == 2:
    joke_label = tkinter.Label (main, text = joke2)
else:
   joke_label = tkinter.Label (main, text = joke3)

可能会起作用

只是一个建议:在 random.randint (1,3) 这样的东西上不需要空格。将其设为 random.randint(1,3) 会更清楚。您的代码中还有一些地方也适用。