169k views
2 votes
1. Using the open function in python, read the file info.txt. You should use try/except for error handling. If the file is not found, show a message that says "File not found"

2. Read the data from the file using readlines method. Make sure to close the file after reading it

3. Take the data and place it into a list. The data in the list will look like the list below

['ankylosaurus\\', 'carnotaurus\\', 'spinosaurus\\', 'mosasaurus\\', ]

5. Create a function called modify_animal_names(list) and uppercase the first letter of each word.

6. Create a function called find_replace_name(list, name) that finds the word "Mosasaurus" and replace it with your name. DO NOT use the replace function. Do this manually by looping (for loop).

The words in the info.text:

ankylosaurus
carnotaurus
spinosaurus
mosasaurus

1 Answer

5 votes

try:

f = open('info.txt', 'r')

except:

print('File not found')

dino_list = []

for line in f.readlines():

dino_list.append(line)

f.close()

def modify_animal_names(list):

for i in range(len(list)):

list[i] = list[i].capitalize().replace('\\', '')

modify_animal_names(dino_list)

def find_replace_name(list, name):

for i in range(len(list)):

if list[i] == name:

list[i] = 'NAME'

find_replace_name(dino_list, 'Ankylosaurus')

This will print out:

['Ankylosaurus', 'Carnotaurus', 'Spinosaurus', 'NAME']

(If you have any more questions, feel free to message me back)

User Gareth McCaughan
by
5.1k points