I want to output the reult of this program onto a txt file, and I also need to a
ID: 3579674 • Letter: I
Question
I want to output the reult of this program onto a txt file, and I also need to add 1 list; 1 dictionary(dictionary value should be a complex object, not just a simple value such as number or string), exceptions (try … except)
class Animal:
animal_type = None
name = None
available_for_adoption = None
def __init__(self, animal_type, name, available_for_adoption):
self.animal_type = animal_type
self.name = name
self.available_for_adoption = available_for_adoption
class PetStore:
animals = None
def __init__(self):
self.animals = []
def add_animal(self, animal):
self.animals.append(animal)
def delete_animal(self, name):
for animal in self.animals:
if animal.name == name:
self.animals.remove(animal)
return True
return False
petstore = PetStore()
choice = input("Pet Store Options ----------- 1. Add Animal 2. Delete Animal 3. Show Animals 4. Exit What would you like to do? ")
while choice!=4:
if choice == 1:
animal_type = raw_input("What type of animal would you like to create? ")
name = raw_input("What is the animal's name? ")
a = raw_input("Is the animal available for adoption?(y/n) ")
if a=='y':
available_for_adoption = True
else:
available_for_adoption = False
animal = Animal(animal_type, name, available_for_adoption)
petstore.add_animal(animal)
elif choice == 2:
name = raw_input("What is the animal's name you'd like to remove? ")
deleted = petstore.delete_animal(name)
if deleted:
print name+" has been removed."
else:
print name+" could not be found"
elif choice == 3:
print "Animal List ----------- "
for animal in petstore.animals: #nested loops
if animal.available_for_adoption==True:
avail = "is available"
else:
avail = "is not available"
print animal.name + " is " + animal.animal_type + " and " +avail
choice = input("Pet Store Options ----------- 1. Add Animal 2. Delete Animal 3. Show Animals 4. Exit What would you like to do? ")
print "Thank you for visiting the Pet Store!"
Explanation / Answer
class Animal:
animal_type = None
name = None
available_for_adoption = None
def __init__(self, animal_type, name, available_for_adoption):
self.animal_type = animal_type
self.name = name
self.available_for_adoption = available_for_adoption
class PetStore:
animals = None
def __init__(self):
self.animals = []
def add_animal(self, animal):
self.animals.append(animal)
def delete_animal(self, name):
for animal in self.animals:
if animal.name == name:
self.animals.remove(animal)
return True
return False
try:
f = open("output.txt", "w")
f.write("Transcript ------------------------------------------------------------------- ")
petstore = PetStore()
choice = input("Pet Store Options ----------- 1. Add Animal 2. Delete Animal 3. Show Animals 4. Exit What would you like to do? ")
f.write("Pet Store Options ----------- 1. Add Animal 2. Delete Animal 3. Show Animals 4. Exit What would you like to do? ")
f.write(str(choice)+" ")
while choice!=4:
if choice == 1:
animal_type = raw_input("What type of animal would you like to create? ")
f.write("What type of animal would you like to create? ")
f.write(animal_type+" ")
name = raw_input("What is the animal's name? ")
f.write("What is the animal's name? ")
f.write(name+" ")
a = raw_input("Is the animal available for adoption?(y/n) ")
f.write("Is the animal available for adoption?(y/n) ")
f.write(" ")
if a=='y':
available_for_adoption = True
else:
available_for_adoption = False
animal = Animal(animal_type, name, available_for_adoption)
petstore.add_animal(animal)
elif choice == 2:
name = raw_input("What is the animal's name you'd like to remove? ")
f.write("What is the animal's name you'd like to remove? ")
f.write(name+" ")
deleted = petstore.delete_animal(name)
if deleted:
print name+" has been removed."
f.write(name+" has been removed. ")
else:
print name+" could not be found"
f.write(name+" could not be found ")
elif choice == 3:
print "Animal List ----------- "
f.write("Animal List ----------- ")
for animal in petstore.animals: #nested loops
if animal.available_for_adoption==True:
avail = "is available"
else:
avail = "is not available"
print animal.name + " is " + animal.animal_type + " and " +avail
f.write(animal.name + " is " + animal.animal_type + " and " +avail+" ")
choice = input("Pet Store Options ----------- 1. Add Animal 2. Delete Animal 3. Show Animals 4. Exit What would you like to do? ")
f.write("Pet Store Options ----------- 1. Add Animal 2. Delete Animal 3. Show Animals 4. Exit What would you like to do? ")
print "Thank you for visiting the Pet Store!"
f.write("Thank you for visiting the Pet Store! ")
f.close()
except IOError:
print "Could not open file"
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.