Here's the question: Using a for loop, write a function called getMax4 that takes in a list of numbers. It determines and returns the maximum number that is divisible by 4. The function returns -999 if the argument is an empty list. The function returns 0 if no number in the argument list is divisible by 4. The following shows sample outputs when the function is called in the python shell:
My code:
# What im trying to do is e.g. let's say:
List=[1,2,3]
maximum=List[0]
for num in List:
if num > maximum:
maximum = num
print(maximum)
by doing the for loop, it first compares with List[0] which is 1, with the "1" in the list. After comparing 1 with 1, there is no differnce, so the max is still 1. Now it moves to the second iteration, maximum=List[0] (which is 1 in the list), now compares with 2 in the list. Now 2 is higher than the maximum, so it updates the new maximum as 2. (sorry for the bad english) So the problem is that when i try to do it with empty set, it gives me index out of range.
Another problem is that when i input the values given in the sample output, all i get is 0.
List=[]
def getMax4 (List):
highest=List[0]
for num in List:
if num % 4 == 0:
if num > highest:
highest = num
return highest
elif num == [] :
return -999
else:
return 0