其他语句始终执行

时间:2018-12-18 01:35:30

标签: python python-3.x tkinter

即使函数显然满足if语句,我的函数也始终在运行else语句。答案是从tkinter文本框返回的text.get()。

def A1Next(Answer):
    print(Answer)
    Answer = str(Answer)
    if Answer == str('print("Hello World!")') or Answer == str("print('Hello World!')"):
        print("Correct")
    else:
        print("Incorrect")

编辑:下面是提供该功能的代码段,用户正在回答问题“打印“ Hello World!”的命令是什么?在python 3.4.4中?”

Q1Title = ttk.Label(Quframe, text = "Question 1", font = ('Helvetica', 10, 'bold'))
Q1Title.grid(row = 0, column = 1, columnspan = 3, stick = 'nsew', padx = 10, pady = 10)
Q1Label = ttk.Label(Quframe, text = "What is the command to print 'Hello World!' in python 3.4.4?", font = ('Helvetica', 10, 'bold')) ##Question user must answer
Q1Label.grid(row = 1, column = 1, columnspan = 3, stick = 'nsew', padx = 10, pady = 10)
# Text box in Window 1
Q1Instructlabel = ttk.Label(Resframe, text = "Enter response below", font = ('Helvetica', 10, 'bold'))
Q1Instructlabel.grid(row = 0, column = 0, columnspan = 3, stick = 'nesw', padx = 10, pady = 10)
Answer1 = Text(Resframe, width = 40, height = 10)
Answer1.grid(row = 1, column = 1, columnspan = 3, stick = 'nsew', padx = 10, pady = 10)
Quitbutton = ttk.Button(Menuframe, text = "Cancel", command = lambda: Window1.destroy()) #Back and next buttons
Quitbutton.grid(row = 0, column = 1, columnspan = 1, stick = 'nsew', padx = 10, pady = 10)
Nextbutton = ttk.Button(Menuframe, text = "Next", command = lambda: A1Next(Answer1.get("1.0", "end"))) #Passes Info to function
Nextbutton.grid(row = 0, column = 3, columnspan = 1, stick = 'nsew', padx = 10, pady = 10)

1 个答案:

答案 0 :(得分:1)

Answer1.get("1.0", "end")将返回以换行符终止的值。您要比较的值不会以换行符结尾,因此比较将始终失败。