Answer:
- 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):")
Step-by-step 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).