Question

In: Computer Science

Scrambled Word Game (Python) Topics: List, tuple In this lab, you will write a scrambled word...

Scrambled Word Game (Python)

Topics: List, tuple

In this lab, you will 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 pick a scrambled word and have the player guess it. The number of guesses is unlimited. When the user guess the correct answer, it asks the user if he/she wants another scrambled word.

bta:bat
gstoh:ghost
entsrom:monster

Download scrambled.py 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:

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

Solutions

Expert Solution

If you have any doubts, please give me comment...

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

    anotherGame = "Y"

    while anotherGame=="Y":

        scramble_word = input("Scrambled word is: ")

        answer_word = input("What is the word? ")

        index = scrambled_list.index(scramble_word)

        if answer_word.lower() != answer_list[index].lower():

            print("Wrong answer. Try again!")

        else:

            print("You got it!")

            anotherGame = input("Another game? (Y/N):")

    print("Bye!")

main()


Related Solutions

Creating a list/tuple in python 2. Using a list of lists containing X’s and O’s to...
Creating a list/tuple in python 2. Using a list of lists containing X’s and O’s to represent a tic tac toe board, write code to check if the board has a winner.
Please write in Python code Write a program that stores the following data in a tuple:...
Please write in Python code Write a program that stores the following data in a tuple: 54,76,32,14,29,12,64,97,50,86,43,12 The program needs to display a menu to the user, with the following 4 options: 1 – Display minimum 2 – Display maximum 3 – Display total 4 – Display average 5 – Quit Make your program loop back to this menu until the user chooses option 5. Write code for all 4 other menu choices
Python pls Create a function party_freq(dicto:dict): this function returns a list inside tuple that how many...
Python pls Create a function party_freq(dicto:dict): this function returns a list inside tuple that how many times each person party in the day. For example def party_freq(dicto:dict) -> [(str,{(int,int,int): int})]: #code here input dict1 ={'fire1': {(2000,5,20,480) : ('Aaron', 25, 300, ( 0, 300)), (2000,5,20,720) : ('Baily', 45, 1500, (1500,500)), (2000,5,21,490) : ('Aaron', 35, 500, (1300,500)) }, 'fire2': {(2000,5,20,810) : ('Baily', 45, 1400, (600,1600)), (2000,5,20,930) : ('Baily', 43, 1800, ( 0, 0)) }} output [('Aaron', {(2000,5,20): 1, (2000,5,21): 1}), ('Baily', {(2000,5,20):...
For this lab, you will work with two-dimensional lists in Python. Do the following: Write a...
For this lab, you will work with two-dimensional lists in Python. Do the following: Write a function that returns the sum of all the elements in a specified column in a matrix using the following header: def sumColumn(matrix, columnIndex) Write a function display() that displays the elements in a matrix row by row, where the values in each row are displayed on a separate line. Use a single space to separate different values. Sample output from this function when it...
Write a program in Python to simulate a Craps game: 1. When you bet on the...
Write a program in Python to simulate a Craps game: 1. When you bet on the Pass Line, you win (double your money) if the FIRST roll (a pair of dice) is a 7 or 11, you lose if it is ”craps” (2, 3, or 12). Otherwise, if it is x ∈ {4, 5, 6, 8, 9, 10}, then your point x is established, and you win when that number is rolled again before ’7’ comes up. The game is...
6.8 LAB: Acronyms ( PYTHON PLEASE ) An acronym is a word formed from the initial...
6.8 LAB: Acronyms ( PYTHON PLEASE ) An acronym is a word formed from the initial letters of words in a set phrase. Write a program whose input is a phrase and whose output is an acronym of the input. If a word begins with a lower case letter, don't include that letter in the acronym. Assume there will be at least one upper case letter in the input. Ex: If the input is: Institute of Electrical and Electronics Engineers...
Write a python program that simulates a simple dice gambling game. The game is played as...
Write a python program that simulates a simple dice gambling game. The game is played as follows: Roll a six sided die. If you roll a 1, 2 or a 3, the game is over. If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again. With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which...
Creating a list/tuple 4. Write a program to accept a string from the user, then replace...
Creating a list/tuple 4. Write a program to accept a string from the user, then replace the first letter with the last letter, the last letter with the middle letter and the middle letter with the first letter. E.g. “Billy” -> “yiBll”.
I have a Python code that reads the text file, creates word list then calculates word...
I have a Python code that reads the text file, creates word list then calculates word frequency of each word. Please see below: #Open file f = open('example.txt', 'r') #list created with all words data=f.read().lower() list1=data.split() #empty dictionary d={} # Adding all elements of the list to a dictionary and assigning it's value as zero for i in set(list1):     d[i]=0 # checking and counting the values for i in list1:     for j in d.keys():        if i==j:           d[i]=d[i]+1 #Return all non-overlapping...
In Java Please!!! 6.22 LAB: Python and sqlite basics Write a Python program that connects to...
In Java Please!!! 6.22 LAB: Python and sqlite basics Write a Python program that connects to a sqlite database. Create a table called Horses with the following fields: id (integer): a primary key and not null name (text) breed (text) height (real) birthday (text) Next, insert the following data row into the Horses table: id: 1 name: 'Babe' breed: 'Quarter Horse' height: 15.3 birthday: '2015-02-10' Output all records from the Horses table. Ex: With the above row inserted, the output...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT