Welcome to Westonci.ca, the ultimate question and answer platform. Get expert answers to your questions quickly and accurately. Join our Q&A platform to connect with experts dedicated to providing accurate answers to your questions in various fields. Get precise and detailed answers to your questions from a knowledgeable community of experts on our Q&A platform.

Project 12-2: Bird Counter
Help me make a Python program for birdwatchers that stores a list of birds along with a count of the
number of times each bird has been spotted.


Console
Bird Counter program

Enter 'x' to exit

Enter name of bird: red-tailed hawk
Enter name of bird: killdeer
Enter name of bird: snowy plover
Enter name of bird: western gull
Enter name of bird: killdeer
Enter name of bird: western gull
Enter name of bird: x

Name Count
=============== ===============
Killdeer 2
Red-Tailed Hawk 1
Snowy Plover 1
Western Gull 2


Specifications
 Use a dictionary to store the list of sighted birds and the count of the number of times
each bird was sighted.
 Use the pickle module to read the dictionary from a file when the program starts and
to write the dictionary to a file when the program ends. That way, the data that's
entered by the user isn't lost.


Sagot :

The program based on the information given is illustrated below.

How to illustrate the program?

It should be noted that a computer program is a set of instructions in a programming language for the computer to execute.

Based on the information, the program is illustrated thus:

Code:

import pickle

#function to read birds data

def readBirdsData():

try:

birdsFile = open("birdsData","rb")

birdWatcher = pickle.load(birdsFile)

birdsFile.close()

birdWatcher ={}

except EOFError:

birdWatcher ={}

return birdWatcher

#function to write the data into file using pickle

def writeBirdsData(birdWatcher):

if birdWatcher == {} :

return

sorted(birdWatcher)

birdsFile = open("birdsData","ab")

pickle.dump(birdWatcher,birdsFile)

birdsFile.close()

#function to display birds data in sorted order

def displayBirdsData(birdWatcher):

print("Name\t\tCount")

print("========\t===========")

for key in sorted(birdWatcher.keys()):

print("{}\t\t{}".format(key,birdWatcher[key]))

#main function

def main():

birdWatcher = readBirdsData()

print("Bird Counter Program")

print ("\nEnter 'x' to exit\n")

name = input("Enter name of bird: ")

while True:

#break the loop if x is entered

if name == 'x':

#if the name exists in dictionary then increase the value

if the name in birdWatcher.keys():

birdWatcher[name] = birdWatcher[name] + 1

else:

#dd it otherwise

birdWatcher[name] = 1

name = input("Enter name of bird: ")

displayBirdsData(birdWatcher)

writeBirdsData(birdWatcher)

#driver code

if __nam__ == '__main__':

main()

Learn more about programs on:

https://brainly.com/question/26642771

#SPJ1

We hope you found what you were looking for. Feel free to revisit us for more answers and updated information. Thanks for stopping by. We strive to provide the best answers for all your questions. See you again soon. We're here to help at Westonci.ca. Keep visiting for the best answers to your questions.