即使两个参数都为真,语句是否返回False?

时间:2019-11-13 02:32:24

标签: python if-statement tkinter

说明:所以我试图检查两个变量(是否相等),然后更改游戏点。我遇到的问题是,即使两个值都为true,if语句也将返回false:“ if str(word)== str(guess):”。我对为什么会这样感到非常困惑,希望有人能给出答案谢谢!!

所有代码:

from tkinter import *
import random, time

root = Tk()
root.title("Anagrams")

#### MODLE (Data,Methods) ####
points = 0
word = ""
word_shuffled = ""
guess = ""
difficulty = 0
words = [["hug", "its", "leg", "lee", "mad", "map", "mug", "nap", "nay", "won", "wed"],
        ["cone", "cool", "crap", "dads", "deaf", "dear", "dial", "dice", "maps", "mace"],
        ["dairy", "dikes", "dozen", "fiber", "eager", "exist", "field", "flesh", "fuels", "glare"],
        ["expect", "former", "filler", "framed", "friend", "fringe", "heater", "hocked", "hustle", "insect"]]

def start():
    global difficulty, points
    update_points()
    get_word()
    difficulty = 0
    points = 0
    root.after_cancel(get_word)

def get_word():
    global word, difficulty, word_shuffled
    if difficulty <= 3:
        word = words[difficulty][random.randint(0,9)]
        word_shuffled = shuffle_word(word)
        output.config(state=NORMAL)
        output.delete('1.0', END)
        output.insert(END, "You letters to make an anagram with: " + str(word_shuffled))
        output.see(END)
        output.config(state=DISABLED)
        root.after(10000, change_dificulty)
    else:
        root.after_cancel(get_word)
        end()


def enter_word():
    global guess, points, word, guess
    guess = str(textenter.get('1.0', END))
    print(word + " " + guess)
    if str(word) == str(guess):
        points += ((difficulty + 1) * 10)
        root.after_cancel(get_word)
        change_dificulty()
    else:
        points -= ((difficulty + 1) * 10)
    update_points()

def change_dificulty():
    global difficulty
    difficulty += 1
    get_word()

def shuffle_word(word1):
    word1 = list(word1)
    random.shuffle(word1)
    return ''.join(word1)

def end():
    global points
    output.config(state=NORMAL)
    output.delete('1.0', END)
    output.insert(END, "You final points are: " + str(points))
    output.see(END)
    output.config(state=DISABLED)

def update_points():
    global points
    point.config(state=NORMAL)
    point.delete('1.0', END)
    point.insert(END, str(points))
    point.see(END)
    point.config(state=DISABLED)

#### Controlers (Widgets that change data) ####
start = Button(root, text="Press to Start", command=start)
start.grid(row=0, column=0, sticky="W")

enter = Button(root, text="Press to Enter", command=enter_word)
enter.grid(row=0, column=9)

textenter = Text(root, height=1, width=10)
textenter.grid(row=0, column=10, sticky="W")

label = Label(root, text="Points:")
label.grid(row=0, column=4, sticky="E")
point = Text(root, height=1, width=5)
point.grid(row=0, column=5)
point.insert(END, points)
point.see(END)
point.config(state=DISABLED)

#### VIEW (Widgets that display outputs) ####
output = Text(root, width=60, height=1)
output.grid(row=1, column=0, columnspan="11")
output.insert(END, "Points: 3 Letters:+/-10, 4 Letters:+/-20 etc...")
output.see(END)
output.config(state=DISABLED)


root.mainloop()

我遇到问题的地方:

def enter_word():
    global guess, points, word, guess
    guess = str(textenter.get('1.0', END))
    print(word + " " + guess)
    if str(word) == str(guess):
        points += ((difficulty + 1) * 10)
        root.after_cancel(get_word)
        change_dificulty()
    else:
        points -= ((difficulty + 1) * 10)
    update_points()

字符串“ guess”和“ word”的输出:

D:\Devlopment\Python\AP Comp Project #1\venv\Scripts\python.exe" C:/Users/lucia/Desktop/main.py
hug hug

1 个答案:

答案 0 :(得分:3)

您的输出具有误导性,因为复制时,您剪切了换行符!

来自this answer

  

[tkinter]的唯一问题是它实际上在我们的输入中添加了换行符。

在检查字符串之前删除换行符!

guess.strip('\n')

guess.rstrip()