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
Write a python script to calculate the average length of the game, Shortest game, longest game...
Write a python script to calculate the average length of the game, Shortest game, longest game and overall length of the game we need a python script that calculates the average length of a snake and ladder game. ie the number of moves it takes a single player to win the game. Then you run the game several times and will compare the results to come up with the required data(length of the game and number of moves )
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...
Write a program in PYTHON for a (very) rudimentary shooter "game". You are the only shooter...
Write a program in PYTHON for a (very) rudimentary shooter "game". You are the only shooter and you start with ammo of 10. The one enemy doesn't shoot back and starts with health of 5. Code a custom function named shoot that prints "Shot fired" and returns True for a hit or False for a miss. Generate a random 0 to assign False or 1 to assign True. In the main function, use a while loop that runs the shoot function...
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...
write a python script for rock scissors paper game
write a python script for rock scissors paper game
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT