Question

Use python write

Thank you so much!


Welcome to animal shelter management software version 1.0 Type one of the following options adopt a pet adopt intake add more animals to the shelter list. display all adoptable pets quit exit the program save the current data transfer transfer pets to another shelter option? list animal type? all alyson dog) chester 1.5 cat) felice 16.0 cat) 14.0 dog) merlin cat) 12.0 cat) Percy, 18.0 cat) Type one of the following options adopt adopt a pet intake add more animals to the shelter list. display all adoptable pets quit exit the program save the current data transfer transfer pets to another shelter option? transfer file name to transfer. txt one of the following options adopt dopt a pet intake add more animals to the shelter list. display all adoptable pets quit exit the program save the current data transfer transfer pets to another shelter option? list s or all? cat cats chester cat) felice 16.0 cat) merlin 5.0 cat) 12.0 cat) percy 18.0 puppet cat) one of the following options adopt dopt a pet intake add more animals to the shelter list display all adoptable pets quit exit the program. save the current data Save transfer transfer pets to another shelter option? quit 5 pets currently in the shelter adopted O transferred This program focuses on using lists that change size and also demonstrates a type of program that maintains data in manner useful in supporting typical volunteer organizations. Turn in a file named anima shelter .py on the Homework section of the course web site. Program dog alyson 5.5 Description: cat Chester 1.5 cat felice 16 In this assignment you will DOG j esse 14 write a program that keeps cat merLIN 5 track of animals at an cat percy 12 animal shelter. at PUPPET 18 parrot sue 2.3 The hope is that all an at a shelter will be adopted soon after entering. However, quick adoptions dont always happen. To speed up adoption and to avoid overcrowding, a shelter may transfer animals to another shelter. When your program starts, it will read from three input files. Each file contains information about pets in the format of the file shown above. The file pets.txt contains the pets currently at the shelter, transferred txt contains the pets that have been transferred from this shelter to another and adopted.txt contains the pets that have been adopted from this shelter. The contents of the files will be in alphabetical order by pet name. Specification of options: adopt: When the user types this option, the program should prompt for the type of pet and then prompt for the pets name as shown in the sample output. This pet should then be removed from the shelter and added to the list of pets that have been adopted. If a pet of this type and name does not exist, the program should print out a not found message as shown in the sample output. intake: When the user types this option, the program should prompt the user for a file name as shown in the sample output. This file will be in the same format as the files read in at the beginning of the program an also be alphabetized by name. Your program should add all of the pets from this file to the shelter list, making sure to maintain the sorted order of the shelter list. list: When the user types this option, the program should prompt the user for a type of pet (cat, dog, hamster, etc). The program should then display a list of all pets at the shelter of that type. If the user enters all instead of a specific type, all pets should be displayed.

media%2Fa3d%2Fa3d81df5-4700-4fac-bf63-8c

pets.txt

dog alyson 5.5
cat chester 1.5
cat felice 16
dog jesse 14
cat merlin 5
cat percy 12
cat puppet 18

to_transfer.txt

cat merlin 5
cat percy 12

intake.txt

bird joe 3
cat sylvester 4.5

the website is https://www2.cs.arizona.edu/classes/cs110/spring17/homework.shtml

0 0
Add a comment Improve this question Transcribed image text
Answer #1

animal_shelter.py

from operator import itemgetter

#create lists required
pets = []  # pets currently in shelter
to_transfer = [] # pets already transferred to another shelter
adopted = [] # pets adopted from this shelter

#read files to fill up the lists with tuples
file = open("pets.txt", "r")
for line in file:
    pets.append(tuple(line.split()))

#print(pets)

file = open("adopted.txt", "r") # you have not provided adopted file so it shows empty list for now
for line in file:
    adopted.append(tuple(line.split()))

#print(adopted)

file = open("to_transfer.txt", "r")
for line in file:
    to_transfer.append(tuple(line.split()))

#print(to_transfer)

while(1):
    input_option=input("""Type one of the following options:
    adopt: adopt a pet
    intake: add more animals to the shelter
    list: display all adoptable pets
    quit: exit the program
    save: save the current data
    transfer: transfer pets to another shelter
    option?""")

    print(input_option)
    if (input_option=="adopt"):
        type = input("cats, dogs or all?")
        petname = input("pet name?")
        flag = False # flag to indicate that pet was not found
        for pet in pets:
            if type == pet[0] and petname == pet[1]:
                pets.remove(pet) # remove this pet from shelter
                #removing pet doesn't hurt sorting order
                adopted.append(pet) #add this pet to adopted list

                # sort adopted list using item 1 that is name of pets
                adopted = sorted(adopted, key=itemgetter(1))

                # write sorted adopted list back to file in right format
                # you can do this in save
                with open('adopted.txt', 'w') as fp:
                    fp.write('\n'.join('%s %s %s' % x for x in adopted))

                #also update pets file
                with open('pets.txt', 'w') as fp:
                    fp.write('\n'.join('%s %s %s' % x for x in pets))
                flag = True
        if not flag:
            print("pet not found")

    elif(input_option=="intake"):
        filename = input("file name?")
        file = open(filename, "r")
        for line in file:
            pets.append(tuple(line.split())) # add all pets in intake to shelter


        # sort pets list using item 1 that is name of pets
        pets = sorted(pets, key=itemgetter(1))

        # write sorted pets list back to file in right format
        with open('pets.txt', 'w') as fp:
            fp.write('\n'.join('%s %s %s' % x for x in pets))
        flag = True
        print("sorted list=")
        print(pets)


    #list all if option is all or selectively list according to type.
    elif (input_option == "list"):
        type = input("cats, dogs or all?")
        if(type=='all'):
            for pet in pets:
                print(pet)
        else:
            for pet in pets:
                if(type == pet[0]):
                    print(pet)

    elif(input_option=="quit"):
        # you can add counters to the above mentioned options and print them
        print("print counters here..")
        break
    elif (input_option == "save"):
        print("save")
        #implement the save using file writing described above.
    elif (input_option == "transfer"):
        print("transfer")
        file = input("file name?")
        #implement like adopted and intake
    else:
        print("invalid input. choose again")
        continue


=========================================================================
​OUTPUTS
1) listRun animal shelter | C:\Users\shiva\AppData\Local\Programs\Python, Python35-32\python.exe Type one of the following options:

2) adopt

Type one of the following options: adopt: adopt a pet intake: add more animals to the shelter list: display all adoptable pet

pets.txt after adopt

乌 B adopted.txt × | B at animal shelter.py × pets.txt × dog alyson 5.5 cat felice 16 cat merlin 5 cat percy 12 cat puppet 1

adopted.txt after adopt

adopted.tkt x animal she cat chester 1.5 dog jesse 14

3) Intake

un:animal_shelter animal_ shelter animal_shelter C:\Users\shiva\AppData\Local\Programs\Python\Python35-32\python.exe Type one

pets.txt after intake (taking care of sorted order with names of pets)

animal _shelter.py adopted.txt xpets.txt x dog alyson 5.5 cat felice 16 bird joe 3 cat merlin 5 cat percy 12 cat puppet 18 ca

Add a comment
Know the answer?
Add Answer to:
Use python write Thank you so much! pets.txt dog alyson 5.5 cat chester 1.5 cat felice...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please use C++ write a program to read a textfile containing a list of books. each...

    please use C++ write a program to read a textfile containing a list of books. each line in the file has tile, ... Question: Write a program to read a textfile containing a list of books. each line in the file has tile, ..... write a program to read a textfile containing a list of books. each line in the file has tile, ... Question: Write a program to read a textfile containing a list of books. Each line in...

  • please use python and provide run result, thank you! click on pic to make it bigger...

    please use python and provide run result, thank you! click on pic to make it bigger For this assignment you will have to investigate the use of the Python random library's random generator function, random.randrange(stop), randrange produces a random integer in the range of 0 to stop-1. You will need to import random at the top of your program. You can find this in the text or using the online resources given in the lectures A Slot Machine Simulation Understand...

  • Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one me...

    Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one menu item called Search. Clicking on search should prompt the user using a JOptionPane input dialog to enter a car make. The GUI should then display only cars of that make. You will need to write a second menu...

  • // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given...

    // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given a partially completed program that creates a list of patients, like patients' record. // Each record has this information: patient's name, doctor's name, critical level of patient, room number. // The struct 'patientRecord' holds information of one patient. Critical level is enum type. // An array of structs called 'list' is made to hold the list of patients. // To begin, you should trace...

  • You need not run Python programs on a computer in solving the following problems. Place your...

    You need not run Python programs on a computer in solving the following problems. Place your answers into separate "text" files using the names indicated on each problem. Please create your text files using the same text editor that you use for your .py files. Answer submitted in another file format such as .doc, .pages, .rtf, or.pdf will lose least one point per problem! [1] 3 points Use file math.txt What is the precise output from the following code? bar...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT