Question: Pet Name and Age Script Please refer to the PowerPoint slide for this week as that contains pet info review code that will be valuable

Pet Name and Age Script
Please refer to the PowerPoint slide for this week as that contains pet info review code that will be valuable for allowing students some practice when it comes to creating dictionaries and working with file modes.
Students will be creating a script that contains pet information and the age of the pet. The script will create a dictionary and then reference a separate file called petinfo.txt
The script must be able to allow the user to add a new pet and their age, delete a record, print oldest pet.
Rubric:
Make sure you upload screen captures of the code and the output
Make sure you add a new pet and their age (be creative on the name)- show screen capture
Make sure you delete a pet from original list - show screen capture
Make sure you print the name of the oldest pet - show screen capture let's add some pet names and ages to the petinfo.txt file first! let's add some pet names and ages to the petinfo.txt file first! Now let's create our script
```
def read_data():
data1={}
inFile = open("petinfo.txt","r")
for line in inFile:
fields = line.split(",")
name = fields[0]
age = fields[1]
data1[name]= int(age)
inFile.close()
return data1
data2= read_data()
``` We'll use a loop to provide a menu
while True:
print("Welcome, pleaes review pet info")
print("1. Add or update a pet name record")
print("2. Delete a pet name record")
print("3. Print the name of the oldest pet")
print("4. Exit")
ans = int(input("Enter Your Choice: "))
if ans ==1:
name = input("Enter Name: ")
age = int(input("Enter Age: "))
data2[name]= age
elif ans ==2:
name = input("Enter name: ")
if name in dataz:
del data2[name]
else:
print("name not in dictionary") Create remaining elif statements and also an else statement
```
elif ans ==3:
highest =-1
for key in data2:
if data2[key]> highest:
highest = data2[key]
key1= key
print("The oldest pet on file is: "+ key1+","+str(highest))
else:
break
``` Final Step, run your script and test every menu option to ensure the script works correctly.
Welcome, pleaes review pet info
1. Add or update a pet name record
2. Delete a pet name record
3. Print the name of the oldest pet
4. Exit
Enter Your Choice:
Pet Name and Age Script Please refer to the

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!