即使两个参数都为真,如果语句返回假?

Posted

技术标签:

【中文标题】即使两个参数都为真,如果语句返回假?【英文标题】:If Statement Returning False Even Though Both Arguments Are True? 【发布时间】:2020-03-08 19:14:59 【问题描述】:

解释:所以我试图检查两个变量(它们是否相等),然后改变游戏的点。我遇到的问题是 if 语句:“if str(word) == str(guess):”即使两个值都是 true,也返回 false?我对为什么会发生这种情况深感困惑,希望有人能回答谢谢!

所有代码:

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

【问题讨论】:

另外,如果您不了解 Tkinter Python 库,我不建议您尝试回答这个问题。 你如何断言表达式的计算结果为False 如果我做 print(word == guess) 它返回 false。 对于调试输出使用repr() 而不是str()。它给出了字符串的技术表示。 写题方面,请多加输入输出给我们看看。从问题中不清楚您如何确定该语句的评估结果为假!另外,您说“两个值都是正确的”,这没有多大意义。最后,您可以从您的问题中删除大部分代码,并告诉我们word 是来自预定数组的字符串,guess 是来自 tkinter Text 框的输入。祝你的游戏好运! 【参考方案1】:

您的输出具有误导性,因为当您复制它时,您删除了一个换行符!

来自this answer:

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

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

guess.strip('\n')guess.rstrip()

【讨论】:

我认为您可能是对的,但guess.strip() 似乎没有删除输入? @LucianChauvin 抱歉,修复了strip 命令。注意参数!

以上是关于即使两个参数都为真,如果语句返回假?的主要内容,如果未能解决你的问题,请参考以下文章

条件语句在 if-else-if 阶梯的两个部分都为真

在Postgresql中,分组后,如果列的任何值为false,则返回false。如果所有值都为真/假,则分别返回true / false

php 条件语句

php 条件语句

java 中 和 && 这类的符号都表示啥?

Python学习之路8 - 内置方法