In: Computer Science
Python Programming: Scrambled Word Game
Topics: List, tuple
Write a scrambled word game. The game starts by loading a file containing scrambled word-answer pair separated. Sample of the file content is shown below. Once the pairs are loaded, it randomly picks a scrambled word and has the player guess it. The number of guesses is unlimited. When the user guesses the correct answer, it asks the user if he/she wants another scrambled word.
bta:bat
gstoh:ghost
entsrom:monster
Download scrambled.py (code featured below) and halloween.txt. The file scrambled.py already has the print_banner and the load_words functions. The function print_banner simply prints “scrambled” banner. The function load_words load the list of scrambled words-answer pairs from the file and return a tuple of two lists. The first list is the scrambled words and the second is the list of the answers. Your job is the complete the main function so that the game works as shown in the sample run.
Optional Challenge: The allowed number of guess is equal to the length of the word. Print hints such as based on the number of guess. If it’s the first guess, provides no hint. If it’s the second guess, provides the first letter of the answer. If it’s the third guess, provides the first and second letter of the correct answer. Also, make sure the same word is not select twice. Once all words have been used, the game should tell user that all words have been used and terminate.
Sample run:
__
_
_
_
/ _\ ___ _ __ __ _ _ __ ___ | |__ | | ___ __| |
\ \ / __|| '__|/ _` || '_ ` _ \ | '_ \ | | / _ \ / _` |
_\ \| (__ | | | (_| || | | | | || |_) || || __/| (_| |
\__/ \___||_| \__,_||_| |_| |_||_.__/ |_| \___| \__,_|
Scrambled word is: wbe
What is the word? bee
Wrong answer. Try again!
Scrambled word is: wbe
What is the word? web
You got it!
Another game? (Y/N):Y
Scrambled word is: meizob
What is the word? Zombie
You got it!
Another game? (Y/N):N
Bye!
Provided code (from scramble.py
def display_banner():
print("""
__
_
_
_
/ _\ ___ _ __ __ _ _ __ ___ | |__ | | ___ __| |
\ \ / __|| '__|/ _` || '_ ` _ \ | '_ \ | | / _ \ / _` |
_\ \| (__ | | | (_| || | | | | || |_) || || __/| (_| |
\__/ \___||_| \__,_||_| |_| |_||_.__/ |_| \___|
\__,_|
""")
def load_words(filename):
#load file containing scrambled word-answer
pairs.
#scrambled word and answer are sparated by :
scrambled_list = []
answer_list = []
with open(filename, 'r') as f:
for line in f:
(s,a) = line.strip().split(":")
scrambled_list += [s]
answer_list += [a]
return (scrambled_list, answer_list)
def main():
display_banner()
(scrambled_list, answer_list) =
load_words('halloween.txt')
#your code to make the game
work.
main()
Halloween.txt file.
bta:bat
gstoh:ghost
entsrom:monster
ihtcw:witch
meizob:zombie
enetskol:skeleton
rpamevi:vampire
wbe:web
isdepr:spider
umymm:mummy
rboom:broom
nhlwaeeol:Halloween
pkiumnp:pumpkin
kaoa jlern tcn:jack o lantern
tha:hat
claabck t:black cat
omno:moon
aurdclno:caluldron
Word Game:
def display_banner(): print(""" __ _ _ _ / _\ ___ _ __ __ _ _ __ ___ | |__ | | ___ __| | \ \ / __|| '__|/ _` || '_ ` _ \ | '_ \ | | / _ \ / _` | _\ \| (__ | | | (_| || | | | | || |_) || || __/| (_| | \__/ \___||_| \__,_||_| |_| |_||_.__/ |_| \___| \__,_| """) def load_words(filename): # load file containing scrambled word-answer pairs. # scrambled word and answer are sparated by : scrambled_list = [] answer_list = [] with open(filename, 'r') as f: for line in f: (s, a) = line.strip().split(":") scrambled_list += [s] answer_list += [a] return (scrambled_list, answer_list) def main(): display_banner() (scrambled_list, answer_list) = load_words('halloween.txt') # your code to make the game work. for scramble, answer in zip(scrambled_list, answer_list): while True: print("Scrambled word is: {}".format(scramble)) tried = input("What is the word? ") if tried != answer: print("Wrong answer. Try again!") else: print("You got it!") break to_play = input("Another game? (Y/N):") if to_play.lower() == "n": break else: continue main()
Optional Challenge:
def display_banner(): print(""" __ _ _ _ / _\ ___ _ __ __ _ _ __ ___ | |__ | | ___ __| | \ \ / __|| '__|/ _` || '_ ` _ \ | '_ \ | | / _ \ / _` | _\ \| (__ | | | (_| || | | | | || |_) || || __/| (_| | \__/ \___||_| \__,_||_| |_| |_||_.__/ |_| \___| \__,_| """) def load_words(filename): # load file containing scrambled word-answer pairs. # scrambled word and answer are sparated by : scrambled_list = [] answer_list = [] with open(filename, 'r') as f: for line in f: (s, a) = line.strip().split(":") scrambled_list += [s] answer_list += [a] return (scrambled_list, answer_list) def main(): display_banner() (scrambled_list, answer_list) = load_words('halloween.txt') # your code to make the game work. for scramble, answer in zip(scrambled_list, answer_list): for guess in range(len(answer)): print("Scrambled word is: {}".format(scramble)) tried = input("What is the word? ") if tried != answer: if 0 < guess < len(answer)-1: print("Wrong answer. Try again!") print("Hint: {}".format(answer[0:guess])) elif guess == len(answer)-1: print("Wrong, the correct word is: {}".format(answer)) else: print("Wrong answer. Try again!") continue else: print("You got it!") break to_play = input("Another game? (Y/N):") if to_play.lower() == "n": break else: continue print("All the words have been used !") main()