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.