Ask Question

Create an empty text document name "input. txt" and copy the example contents from below: 5 105 15 20 35 47 The first value in the file tells you how many ints (the type must be int!) are in the file. The rest of the values are the ints you want to store. Create a program that reads in the first value, always an int, that tells you how many values are in the file. Then, create an array to store all of those values. After you've read in the values display them to the screen in the following format: Contents of input. txt: [105, 15, 20, 35, 47] Input a value to search for: The search prompt obtains a value from the user and confirms whether or not it is in the array. Example: Contents of input. txt: [105, 15, 20, 35, 47] Input a value to search for: 5 5 is not in the array. Do you wish to quit (y/n) : n Input a value to search for: 105 105 is in the array. Do you wish to quit (y/n) : y

+3
Answers (1)
  1. 16 January, 00:48
    0
    with open ("input. txt") as file: data = file. read () inputList = data. split (" ") count = int (inputList[0]) numArr = [] for i in range (1, count+1) : numArr. append (int (inputList[i])) print (numArr) search = int (input ("Input a value to search for: ")) if search in numArr: print ("Value found in array") else: print (str (search) + " is not in the array") choice = input ("Do you wish to quit (y/n) : ") while (choice! = "y") : search = int (input ("Input a value to search for: ")) if search in numArr: print ("Value found in array") else: print (str (search) + " is not in the array") choice = input ("Do you wish to quit (y/n) : ")

    Explanation:

    Firstly, we open a file stream to read the text file and use split method to break the input into a list of individual numbers (Line 1 - 3). We set the first item of the inputList to count variable and create a numArr list (Line 4-5).

    Use a for loop to populate the number from inputList to numArr (Line 6-7) and display the number from the list (Line 9).

    Next, prompt user to search for a number from list (Line 11).

    If the input is matched with one of the item in numArr, display found message or display a not found message if the input is not available in the numArr (Line 13 - 16). Prompt the user again to check if the user wish to quit the search (Line 18). If not, repeat the same process to input a search value and check the value is found in the numArr and display appropriate message (Line 21 - 28).
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Create an empty text document name "input. txt" and copy the example contents from below: 5 105 15 20 35 47 The first value in the file ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers