1

I am a beginner and I'm trying to write basic program that should check if the user input (entry) matches with the random word shown (a), or not. If it matches, it should give a new word and the typing field should be cleared.

Here is my code so far:

import random
import requests
import Tkinter
import requests
from Tkinter import *

point = 0

word_site = "http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain"
response = requests.get(word_site)
WORDS = response.content.splitlines()
a = random.choice(WORDS)

root = Tkinter.Tk()
root.title("Stopwatch")
root.minsize(width=900, height=600)
root.configure(background='white')

e1 = Entry(root , justify='center')
e1.place(anchor=CENTER , bordermode=OUTSIDE)
e1.config(bg="white" , font="Geneva 30 bold")
e1.pack(expand=False, padx=20 , pady=20, ipadx=10, ipady=10)

label = Tkinter.Label(root, text = "Write this word: " + a , bg="white" , font="Geneva 30 bold")
label.pack()

if e1 == a: #virker ikke!
    print "correct"
    e1.delete(0, END)


root.mainloop()
foot enjoyer
  • 63
  • 1
  • 1
  • 6

3 Answers3

1

If your problem is only the line marked "virker ikke!" then it is because you are comparing a string value (a) with a tkinter.Entry instance.

Use the "get" method to get the current text from the entry:

if e1.get() == a:
  print("correct")
  e1.delete(0, END)

Secondly, I think you have the wrong idea about how UI toolkits typically work.

You create a text entry instance and display it correctly. But then, immediately after that, you check the value and you only check it once.

Instead you need to setup some signal handling. For example: set a callback to be called when the text in the entry changes, or add a button and attach the same callback to its click event.

Also, you need to let the program enter the main loop before you can expect any input in the entry.

Please note that I'm no expert at tkinter so this is only the general idea.

Jonatan
  • 3,752
  • 4
  • 36
  • 47
0

If both are strings you can use the == operator, like follow:

a == b

Note that this is case-sensitive, if you want it to be case-unsensitive you can use the .lower() function of strings:

a.lower() == b.lower()

If they are not Strings, you can probably cast them to string.

Oskar B
  • 48
  • 1
  • 3
0

Do something like this:

import random

def random_word(words):
    return random.choice(words)

def user_choice():
    return raw_input("Enter a random word: ")

def main():
    words = ["and", "that", "this", "is"]
    computer_generated_word = random_word(words)
    user_picked_word = user_choice()
    if computer_generated_word.lower() == user_picked_word.lower():
        print("Word has successfully been guessed '{}'...".format(computer_generated_word))
    else:
        print("That's not the right word...")

if __name__ == "__main__":
    main()

Now since you said you're new lets break this down for you..

import random

Import the random package, documentation can be found here

def random_word(words):
    return random.choice(words)

Return a random word from a list of words given as a argument to the method using the built-in random package.

def user_choice():
    return raw_input("Enter a random word: ")

Create a method to get user input from stdout by calling the raw_input function (worth mentioning that it's just input after 2.7.x)

def main():
    words = ["and", "that", "this", "is"]
    computer_generated_word = random_word(words)
    user_picked_word = user_choice()
    if computer_generated_word.lower() == user_picked_word.lower():
        print("Word has successfully been guessed '{}'...".format(computer_generated_word))
    else:
        print("That's not the right word...")

Create a main method to compare the two words lowercase version of eachother. If the words match, output they match, if not output they don't.

if __name__ == "__main__":

Python basic practice. Basically used to call the method you want when the program starts

    main()

Call the main method.

13aal
  • 1,634
  • 1
  • 21
  • 47