为什么随机选择相同的选择?

时间:2017-12-03 18:18:54

标签: python python-3.x tkinter

我正在尝试为用户创建一个返回yes或no的代码,但输出始终为yes。

我应该更改或添加什么才能更改答案?

from tkinter import *
from tkinter import ttk
import random

list=["yes", "no"] #a list of answers
answer=random.choice(list) #choosing an answer

root=Tk(className="yes or no") 

lab=ttk.Label(root, text="what's your question" ) 
lab.pack()

#ask user for input
U=ttk.Entry(root) 
U.pack()

def return_answer():
    U.get()
    U.delete(0, END)
    U.insert(0,answer)

B1=ttk.Button(root, text="ok",command=return_answer)
B1.pack()


root.mainloop()

1 个答案:

答案 0 :(得分:2)

删除

answer=random.choice(list) #choosing an answer

并替换

    U.insert(0,answer)

    U.insert(0, random.choice(list))