In: Computer Science
Working with Python. I had to create a jackalope, with half an image of an antelope and half rabbit. Here is my code:
def animalCollage():
antelope = makePicture(pickAFile())
rabbit = makePicture(pickAFile())
antPixel = getPixels(antelope)
rabPixel = getPixels(rabbit)
for index in range(len(antPixel)/2,len(antPixel)):
newColor = getColor(rabPixel[index])
setColor(antPixel[index], newColor)
writePictureTo(antelope, "C:/Users/janin/OneDrive - Grand Canyon
University/Grand Canyon University/CST-111/Week
6/jackalope.jpg")
jackalope = pickAFile()
show(antelope)
My code worked fine, but my instructor said: "the python code to create the jackalope looks fine except I can't run it because you tried to write the picture out to your hard drive. You need to write the picture out to a pickAFile() location."
I am not sure how to correct this. Can anyone please help?
Thank you
Answer
import os #Import os library
def pickAFile():
filename = input("Enter a filename:\n") #Ask user to enter filename
if os.path.exists(filename): #Check if file with the same name already exists
os.remove(filename) #Remove the file
open(filename, 'w+').close() #Create a new file
return os.path.abspath(filename) #Return the file path
path = pickAFile()
print(path)
Output:
Note: Follow indentations carefully!