新手求助一个关于define function的问题
Implement a Python function called validate_image(image) which takes an image as its argument and returns True if the image is valid and False otherwise. An image is valid if it satisfies all the following conditions:• It is a list of lists of integers only (no other types are valid).
• All rows in the image are of the same length (the image is rectangular). • Allintegersiintheimagelieintherange0≤i≤255.
You can test if an object is a list or an integer using the type function like so:
>>> type(1) == int
True
>>> type(True) == int
False
>>> type([1,2,3]) == list
True
>>> type(None) == list
False
Below are some example applications of the valid_image function:
>>> validate_image([]) # the empty image is valid
True
>>> validate_image([1]) # not a list of lists of integers
False
>>> validate_image(None) # not a list at all
False
>>> validate_image([[0,23,97,45], [125,3,12,1], [8,8,8,8]])
True
>>> validate_image([[1,2,3], [4,5]]) # rows different length
False
>>> validate_image([[1,2,3], [4,5,256]]) # 256 > maximum intensity
False