0

Explanation: So I am trying to check if two variables (which are equal) and then changing the points of the game. The problem I am having is that the if statement: "if str(word) == str(guess):" is returning false even though both values are true? I deeply confused about why this is happening and hoping someone has the answer thanks!

All Code:

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()

Where I am having the problem:

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()

Outputs for the strings "guess" and "word":

D:\Devlopment\Python\AP Comp Project #1\venv\Scripts\python.exe" C:/Users/lucia/Desktop/main.py
hug hug
  • Also, I don't recommend trying to answer this if you do not understand the Tkinter Python library. – Lucian Chauvin Nov 13 '19 at 02:35
  • How do you assert that the expression evaluates to `False`? – Klaus D. Nov 13 '19 at 02:40
  • If I do print(word == guess) it returns false. – Lucian Chauvin Nov 13 '19 at 02:42
  • For debug output use `repr()` instead of `str()`. It gives a technical representation of the string. – Klaus D. Nov 13 '19 at 02:46
  • 4
    In terms of question writing, please add more input and output for us to see. It's not clear from the question how you are determining that the statement is evaluating to false! Also, you say that 'both values are true,' which doesn't make much sense. Lastly, you could cut most of the code from your question and just tell us that `word` is a string from a predetermined array and `guess` is the input from a tkinter `Text` box. Good luck with your game! – Mars Nov 13 '19 at 02:47
  • You need to make a [mre]. This is way too much code. Based on the description the problem should be reproduceable in like three lines. – wjandrea Nov 13 '19 at 03:00

1 Answers1

3

Your output is misleading, because when you copied it, you cut out a newline!

From this answer:

The only issue with [tkinter] is that it actually adds a newline to our input.

Remove the newline before checking your strings!

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

Mars
  • 2,505
  • 17
  • 26