Python program that plays a guess-the-number game with magic lists Solved

20.00 $

Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: . zip solution files instantly, after Payment


5/5 - (1 vote)

Write a Python program that plays a guess-the-number game with magic lists.

The program will play as many games as the user wants. In each game, the program asks the user to think of a number between 1 and 31, inclusive. Then the program presents the user with 5 lists and asks the user if their number is in the list. Based on the user’s answers of ‘y’ or ‘n’, the program guesses the user’s number.

  • The program works with 5 files, each file contains 16 integers, one number per line.

The filenames are: magicList1.txt, magicList2.txt, magicList3.txt, magicList4.txt, and magicList5.txt

  • The program is divided into 3 functions.

1. A createList function that will read in data from one magicList file.
The function does the following tasks:

    • Create an empty list.
    • Use the with construct to open the file magicListN.txt, where N is the input argument for the function.
    • Loop to read in each line in the file, convert the line to an integer, and store the integer in the list.
  • Return the list.
  • Note: don’t call any list variable with the name “list”. “list” is a Python data type and is a reserved word. Some common ways to call a generic list variable are: List or _list or list_ or L , if you don’t want to use the more descriptive name magicList.

2. A checkList function that will check whether the user number is in the magic list.
The function does the following tasks:

  • Use a nested loop to print the magic list (16 numbers) as a table of 4 rows and 4 columns.
  • Loop to ask the user if their number is in the list, stop the loop when the user answers ‘y’ or ‘n’
    The condition for the loop must be whether the user answers ‘y’ or ‘n’

3. A main function that will:

  • Loop to create an outer list of magic lists. The loop will:
    • run 5 times to call createList, and each time store the returned magic list into the outer list.
  • Loop to let the user keep playing as long as the user wants:
    • Create an inner loop that runs 5 times:
      • Call checkList to ask the user whether their number is in a particular magic list, passing to checkList one magic list at a time. Recall that the magic lists are elements in the outer list that you created in the step above.
      • If the user answer is ‘y’ and:
        • it’s magicList1, then add 16 to the total
        • it’s magicList2, then add 8 to the total
        • it’s magicList3, then add 4 to the total
        • it’s magicList4, then add 2 to the total
        • it’s magicList5, then add 1 to the total
    • Ask the user if they want to continue, and loop back to play again if the answer is ‘y’
  • When done, if the total is non-zero, then print the user’s number
    If the total is 0, then print an error message
  • Don’t forget to call the main function so that the program will run.

C. (10 pts) Test your output by running several games, one after another.