-5

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:

This is the sample output

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
L3viathan
  • 26,748
  • 2
  • 58
  • 81
  • 2
    You appear to have forgotten to ask a question. All we have is your assignment and your code. Nothing about what you do with that code, or what happens when you run it. Or what you want us to help with for that matter. – Martijn Pieters Jul 30 '16 at 16:50
  • i use wingide. how do i attach a photo ? – Heeh wei cheng Jul 30 '16 at 16:52
  • The problem you're having occurs because you immediately return `highest` once a divisible number is found. Instead, initialize `highest` with 0, `return highest` outside the for loop, and check for the `List` being empty (outside the for loop), not the number. – L3viathan Jul 30 '16 at 16:54
  • @L3viathan could you show me a picture of how you do it ? – Heeh wei cheng Jul 30 '16 at 16:57
  • @Heehweicheng Once you edit your question to include what you have problems with exactly, and what you've tried to resolve them. – L3viathan Jul 30 '16 at 16:59
  • @L3viathan But if i put the return highest outside the for loop, the next line of code highlights that "warning your code may not be reached." – Heeh wei cheng Jul 30 '16 at 17:52
  • @MartijnPieters i have edited my question – Heeh wei cheng Jul 30 '16 at 18:11
  • @Heehweicheng And that is true: if it returns -999 or 0 it won't reach that code. IDEs are a useful tool, but don't trust them blindly. – L3viathan Jul 30 '16 at 22:32

2 Answers2

0

Your first problem is at highest = List[0]. Since List is not guaranteed to have at least one item, you will have an error if List is empty. You should add a check:

def getMax4(List):
    if not List: # or if len(List) == 0:
        return -999
    ...

Another problem is that you define highest as the first item. Why? The first item could possibly be the highest only if it is divisible by four. You should set it to 0 so that even if there are no numbers divisible by four, it will still have the default of 0 that is required in the examples.

The next problem is that you are returning inside the loop. Wait until the loop is done before you try to return.

Once we fix all those, the problem is that -4 is better than 0 even though it is lower. We can simply add a check for not highest (highest is equal to 0). The new code looks like this:

def getMax4(List):
    if not List:
        return -999
    highest = 0
    for num in List:
        if num % 4 == 0 and (num > highest or not highest):
            highest = num
    return highest
zondo
  • 19,901
  • 8
  • 44
  • 83
  • Thanks for the clear explanation ! i understood fully now that i should have checked how many elements are in the List using the len() function. Luckily, i have learnt the logical operators in my lesson "and" and "or" today if not i won't understand what you said. – Heeh wei cheng Jul 31 '16 at 05:45
  • but i don't understand what's the meaning of not List. I think i don't understand the function of "not". – Heeh wei cheng Jul 31 '16 at 05:59
  • Let's say you have `True`. The opposite of that, `not True` is `False`. Similarly, `not False` is `True`. Well, more than just `True` and `False` have boolean opposites. An empty string is, in a way, equivalent to `False`. Therefore, `not ""` is `True`. Similarly, `not []` is `True`, but `not [1]` or `not [3, 6]`, etc. are `False`. In other words `if not List` is a fancy way of saying `if len(List) == 0`. Please see http://stackoverflow.com/q/53513/5827958 – zondo Jul 31 '16 at 06:04
0

In regards to your 2nd piece of code you need to be mindful of where you place your returns. A return statement will exit the function with the value. Therefore these checks should be done before and after the loop as your function requires it to go through the entire list.

I'm not too fond of setting your initial value to something that could be a valid output. Therefore here is my code where I use None for the case of no current max. However it requires an extra check at the end since 0 is expected.

def getMax4(lst):
    if not lst:
        return -999

    highest = None
    for num in lst:
        if num % 4 == 0 and (highest is None or num > highest):
            highest = num
    return 0 if highest is None else highest


>>> getMax4([])
-999
>>> getMax4([1, 3, 9])
0
>>> getMax4([-4, 3, -12, -8, 13])
-4
>>> getMax4([1, 16, 18, 12])
16
>>> getMax4([-4, 0, -8])
0

In the last example 0 is the max value, if 0 should not be counted as a valid maximum divisible by 4 then an extra condition is required for the if statement.

Steven Summers
  • 5,079
  • 2
  • 20
  • 31