Write a tkinter game Scrambler, that allows the user to guess a scrambled word.
ID: 3666740 • Letter: W
Question
Write a tkinter game Scrambler, that allows the user to guess a scrambled word. The game is started by giving a word and packing. The parent can be specified or omitted, e.g, either:
>>> Scrambler("apple").pack()
>>>
Or
>>> root = Tk()
>>> Scrambler("orange",root).pack()
>>>
The game will then scramble the word and display it in the gui, see images to the right. The user will then have 3 guesses to guess the original word. (To scramble a word, make sure you add the import “from random import *”, then use the following code.
scramble = list( word )
shuffle(scramble)
scramble = ''.join( scramble) # this is now the scrambled word
To guess a word, the user types in the Entry and clicks the button. The game then checks the guess against the original. There are three possibilities:
The guess is correct, then the user is told “You got it” in a showinfo.
The guess is incorrect, and there is at least 1 guess left. Then the user is told the number of guesses left.
The guess is incorrect, and no guesses are left. The user is told “You lose”
ppael GuessExplanation / Answer
import random
#Creates a dictionary for the words
words = {'lights':'Something with multiple colors on a Christmas tree',
'santa':'Fat old man who comes around Christmas',
'elves':'Little people with pointy ears that work with Santa',
'tree':'The thing people put presents under on Christmas',
'snow':'Frozen yet soft form of rain',
'ornaments':'Accessories that get hung on a Christmas tree',
'sleigh':'The vehicle that Santa rides in to bring toys to all the world',
'christmas':'December 25th',
'present':'A kid opens one on Christmas',
'snowball':'The frozen and soft version of rain formed into a shape',
'snowman':'Frosty the ________',
'reindeer':'Rudolph the red-nosed _______',
'jesus':'The person that Christmas is named for',
'manger':'What Jesus was born in',
'stars':'The bright lights in the sky at night',
'grinch':'Green guy from Who-ville',
'stockings':'Looks like a sock, people use these to hold small gift on Christmas',
'rudolph':'Reindeer with a bright-red nose',
'dasher':'One of Santa's reindeer',
'garland':'Think tinsel for a Christmas tree'}
#Function that prints the COP 1000 line and briefly explains the game
def Intro():
print('COP 1000 Project 5 - Charles Goggins ')
print('Word Scramble! ')
print('I'm going to show you a scrambled word and give you three guesses.')
print('After your second guess, I'll help by giving you a hint. ')
#Selects a word from the dictionary at random and returns it it's caller
def getWord(word):
wordKey = random.choice(list(word.keys()))
return wordKey
#When the player guesses incorrectly twice, the program will provide a hing
def getHint():
hint = len(wordKey.index)
return hint
#Gets the first guess from the player, also ensures only characters are entered
def getGuess1():
guess = input(' Enter your first guess: '
while True:
if guess.isalpha():
return guess
else:
guess = input(' Please enter a letter: ')
#Does the same as getGuess1() but gets the player's second guess only if he/she didn't guess correctly the first time
def getGuess2():
guess = input(' Enter your second guess: ')
while True:
if guess.isalpha():
return guess
else:
guess = input(' Please enter a letter: ')
#Does the same as getGuess2() but gets the player's third guess only if he/she didn't guess correctly the first and second time
def getGuess3():
guess = input(' Enter your final guess: ')
while True:
if guess.isalpha():
return guess
else:
guess = input(' Please enter a letter: ')
#When the player types y to the question and then types HELPME or HELP in the prompt, the program will provide a hint
def cheatCode(yes, werd):
x = werd[0]
y = werd[1]
print(x, end = '')
print(y)
return x, y
#==============================================================MAIN PROGRAM===============================================#
#Initializes some varibles, ans = 'y' is for the play again loop
ans = 'y'
guesses = 0
while ans == 'y':
Intro()
#Calls the getWord() function
w0rd = getWord(words)
#Turns w0rd into a list to enable scrambling
scramWord = list(w0rd)
#Scrambles the word
random.shuffle(scramWord)
#Joins the scrambled word together to be able to display it
sWord = ''.join(scramWord)
print('The scrambled word is ' +sWord)
#Calls the getGuess1() function and determines whether the player guessed correctly
guessed = getGuess1()
if guessed == w0rd:
print('Congratulations!')
ans = 'y'
if guessed != w0rd:
print('Nope, sorry')
#Counts the amount of guesses taken
guesses = guesses + 1
cheat = input('Do you want help? [y/n] ')
if cheat == 'y' or cheat == 'Y':
cheatInput = input('Type "HELPME or HELP": ')
cheatCode(cheatInput, w0rd)
#Calls the getGuess2() function
guessed2 = getGuess2()
if guessed2 == w0rd:
print('Congratulations!')
ans = 'y'
if guessed2 != w0rd:
print('Nope, sorry')
guesses = guesses + 1
cheat = input('Do you want help? ')
if cheat == 'y' or cheat == 'Y':
cheatInput = input('Type "HELPME or HELP"')
cheatCode(cheatInput, w0rd)
#Code to provide the hint
while guesses == 2:
hint = words[w0rd]
156
print('Heres a hint ===> '+hint)
guesses = guesses + 1
guessed3 = getGuess3()
if guessed3 == w0rd:
print(' Congratulations! ')
else:
if guessed3 != w0rd:
print(' Nope, sorry. The word was ''+w0rd+'' Better luck next time')
guesses = 0
#Code to ask the player if he/she wants to play again
print(' would you like to play again?[y/n]', end = ' ')
ans = input()
if ans == 'n' or ans == 'N':
print(' Goodbye then')
Is This A Good Question/Topic? 1 +
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.