0

can someone help me am trying to create a game that scrambles words then you have guess the corect words for it this what i have doen so far but it dint seem to work can sometell me what the problem.

import tinker
import random

words_list = []
f = open("words.txt")
for thing in f:
    words_list.append(thing[:-1])

def jumble_word(word):
    # create a list of characters of the word
    char_list = list(word)
    # shuffle sequence in place
    random.shuffle(char_list)
    # joint to form a word again
    return "".join(char_list)
# create a list of jumbled words
jumbles = []
for word in words_list:
    jumbles.append(jumble_word(word))

# create a dictionary from the two lists
words_dict = dict(zip(words_list, jumbles))

global tot_words
tot_words = len(words_dict)
global correct
correct = 0
# the flag variable will ensure the correctful working of score system
# otherwise, if player had got a question right, pressing "Guess" button multiple times
# would have incremented the score that many times until the "Next" button was pressed
global flag

def func():
    'This is the core function of the program'
    # The result of popitem() method is a tuple
    # The following is an example of "sequence unpacking"
    word, jumbled = words_dict.popitem()
    return word, jumbled

def guess(even):
    ans = input_word.GetValue()
    global flag
    global correct
    if(ans == query[0]):
        result.SetLabel(label="Congrats! You got it right.")
    # remind this function that "correct" is a global variable
if(flag==1):
        correct+=1
score.SetLabel(label="Your score is: "+str(correct)+"/"+str(tot_words)
 flag ==0
    else:
        result.SetLabel(label="Sorry, wrong answer. Better luck next time!")
    flag = 1

def next(event):
    # After a person clicks the Start button for the first time, this will happen
    nextButton.SetLabel("Next")
    guessButton.Enable()
    hintButton.Enable()
    input_word.SetValue("")

    global query
    query = func()
    if(words_dict!={}):
        question.SetLabel(label="The jumbled word is: "+query[1])
    result.SetLabel(label="Waiting for your input...")
    global flag
    flag = 1
    else:
        question.SetLabel(label="Game Over!")
        result.SetLabel(label="Yup, the game is finally over!")

#create a GUI window.
root = tkinter.Tk()
#set the title.
root.title("jumbled Game")
root.mainloop()
2
Contributors
1
Reply
17
Views
2 Years
Discussion Span
Last Post by pyTony
0

global tot_words
tot_words = len(words_dict)
global correct
correct = 0

These declarations do not make sense as they are on top level, not inside function. If you must use globals, put them at very beginning of functions. Usually classes are better option.

I rearrange your lines standard way and commented some strange lines there.

# imports
import tinker
import random

# functions
def jumble_word(word):
    # create a list of characters of the word
    char_list = list(word)
    # shuffle sequence in place
    random.shuffle(char_list)
    # joint to form a word again
    return "".join(char_list)

def func():
    'This is the core function of the program'
    # The result of popitem() method is a tuple
    # The following is an example of "sequence unpacking"
    word, jumbled = words_dict.popitem()
    return word, jumbled

def guess(even):
    global flag
    global correct
    ans = input_word.GetValue()
    if(ans == query[0]):
        result.SetLabel(label="Congrats! You got it right.")
    # remind this function that "correct" is a global variable

# bad name next is occupied word, you are shadowing it's normal meaning after this function definition
def next(event):
    # After a person clicks the Start button for the first time, this will happen
    global flag
    global query
    nextButton.SetLabel("Next")
    guessButton.Enable()
    hintButton.Enable()
    input_word.SetValue("")

    query = func()
    if(words_dict!={}):
        question.SetLabel(label="The jumbled word is: "+query[1])
    # if finished here as next lines are unindented
    result.SetLabel(label="Waiting for your input...")
    flag = 1
    ## ?? this else is outside of if, syntax error 
    else:
        question.SetLabel(label="Game Over!")
        result.SetLabel(label="Yup, the game is finally over!")

# top level lines

words_list = []
f = open("words.txt")
for thing in f:
    words_list.append(thing[:-1])

# create a list of jumbled words
jumbles = []
for word in words_list:
    jumbles.append(jumble_word(word))

# create a dictionary from the two lists
words_dict = dict(zip(words_list, jumbles))
tot_words = len(words_dict)
correct = 0
# the flag variable will ensure the correctful working of score system
# otherwise, if player had got a question right, pressing "Guess" button multiple times
# would have incremented the score that many times until the "Next" button was pressed


# I do not understand indention here and I am quite sure the computer also does not
if(flag==1):
        correct+=1
score.SetLabel(label="Your score is: "+str(correct)+"/"+str(tot_words)
 flag ==0
    else:
        result.SetLabel(label="Sorry, wrong answer. Better luck next time!")
    flag = 1


#create a GUI window.
root = tkinter.Tk()
#set the title.
root.title("jumbled Game")
root.mainloop()

Edited by pyTony

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.