129k views
2 votes
Python help with the program that will allow bird watchers to enter how many of each bird is sighted, store the data, and display the data. Follow the following functions, called createData, readData, and main().

1. Requires 1 parameter: file name.

2. Opens the file for writing.

3. Asks a bird’s name.

4. Start while loop: (as long as the name entered is not stop)oGets the bird’s count. An integer.oWrites the bird’s name to the file defined by the parameter.oWrites the bird’s count to the file defined by the parameter.oCounts how many records were written to the file.oGets a bird’s name. (Ask user to enter stop as the name when there are no more birds to enter.)

5. Closes the file.

6. Returns the record count.readData: This function reads the file, prints the contents, and returns the count

1. Requires 1 parameter: file name.

2. Opens the file for reading.

3. Reads 1st record as the bird type.

4. Starts while loop: (As long as the type is not "")oReads in the bird’s count.oPrints the name and count on the same line with a tab between them.oKeeps track of how many types and how many birds.oReads next record as the bird type.

5. Returns the total number of birds sighted and the number of bird types.main():

1.Asks whether the user wants to create a file or read the file. Make sure no other options are accepted. (Validation loop)

2.Asks for the file name.

3.Calls the appropriate function based on the option entered.

4.If the option was to write, prints "You wrote x records to the file.

"5.If the option was to read, prints "There were x types of birds and y total birds."

1 Answer

0 votes

A Python program for bird watchers allows entering bird sightings, storing data in a file, and displaying the records. Options include creating or reading a file, providing relevant statistics.

Here is a simple Python program based on your specifications:

```python

def createData(file_name):

try:

with open(file_name, 'w') as file:

record_count = 0

bird_name = input("Enter bird's name (type 'stop' to end): ")

while bird_name.lower() != 'stop':

bird_count = int(input(f"Enter count for {bird_name}: "))

file.write(f"{bird_name}\t{bird_count}\\")

record_count += 1

bird_name = input("Enter bird's name (type 'stop' to end): ")

print(f"You wrote {record_count} records to the file.")

except Exception as e:

print(f"Error: {e}")

def readData(file_name):

try:

with open(file_name, 'r') as file:

bird_types = 0

total_birds = 0

bird_type = file.readline().strip()

while bird_type != "":

bird_count = int(file.readline().strip())

print(f"{bird_type}\t{bird_count}")

bird_types += 1

total_birds += bird_count

bird_type = file.readline().strip()

return bird_types, total_birds

except Exception as e:

print(f"Error: {e}")

return 0, 0

def main():

while True:

print("Options:")

print("1. Create a file")

print("2. Read the file")

option = input("Choose an option (1 or 2): ")

if option == '1':

file_name = input("Enter the file name: ")

createData(file_name)

break

elif option == '2':

file_name = input("Enter the file name: ")

bird_types, total_birds = readData(file_name)

print(f"There were {bird_types} types of birds and {total_birds} total birds.")

break

else:

print("Invalid option. Please choose 1 or 2.")

if __name__ == "__main__":

main()

```

This program defines three functions as per your requirements (`createData`, `readData`, and `main`). The `main` function includes a validation loop to ensure that the user enters a valid option. The program then calls the appropriate function based on the user's choice and displays the required output.

User Natanya
by
8.2k points