Card Games exercise

I constantly get this errror that ‘int’ object is not iterable, but in the test result the elements of the list are also lists and are not int type, how can I fix it?

def approx_average_is_average(hand):
    """Return if the (average of first and last card values) OR ('middle' card) == calculated average.
    average = card_average(hand)
    first_way = (card_average(hand[0]) + card_average(hand[-1]))/2
    med = (len(hand)+1)/2
    sec_way = hand[med]
    
    return average == first_way or average == sec_way

Code Run

input_data = [[0, 1, 5], [3, 6, 9, 12, 150], [1, 2, 3, 5, 9],
              [2, 3, 4, 7, 8], [1, 2, 3], [2, 3, 4],
              [2, 3, 4, 8, 8], [1, 2, 4, 5, 8]]

result_data = [False, False, False, False, True, True, True, True]

for variant, (hand, expected) in enumerate(zip(input_data, result_data), start=1):
    with self.subTest(f'variation #{variant}', hand=hand, expected=expected):
        actual_result = approx_average_is_average(hand)
        error_message = (f'Called approx_average_is_average({hand}). '
                         f'The function returned {actual_result}, but '
                         f'the hand {hand} {"does" if expected else "does not"} '
                         f'yield the same approximate average.')

        self.assertEqual(actual_result, expected, msg=error_message)

Test Failure

TypeError: ‘int’ object is not iterable

The test has a list of inputs. For each input, it calls your function. Your function should expect a list of ints and not a list of lists.

1 Like

I changed the syntax and yet I still get errors, the code seems fine to me I don’t which part I’m doing wrong :grimacing:

def approx_average_is_average(hand):
  
    average = card_average(hand)
    first_way = (hand[0] + hand[-1])/2
    med = (len(hand)+1)/2
    sec_way = hand[med]

    
    return average == first_way or average == sec_way

Test Failure

AssertionError: False != True : Called approx_average_is_average([1, 2, 4, 5, 8]). The function returned False, but the hand [1, 2, 4, 5, 8] does yield the same approximate average.

  1. Do you understand what the test is doing?
  2. Do you understand what the test is expecting and why?
  3. Do you understand what your code returns and how it differs?
  4. Do you understand why your code is returning what it returns?

From what I can see, the syntax and logic seem correct. The issue appears to be in the last line of the code, where a condition for the output is specified.

  1. Do you understand what the test is doing?
  2. Do you understand what the test is expecting and why?
  3. Do you understand what your code returns and how it differs?
  4. Do you understand why your code is returning what it returns?

It would be helpful if you responded to each point here to help identify the issue.

  1. The test uses a list of numbers as input to calculate the average using two suggested methods.
  2. The test expects that at least one, or both, of the methods for calculating the average will produce the actual average.
  3. Based on my observation of the test failure, the output is giving the opposite answer.
  4. Unfortunately, I don’t understand this part.

You’re generalizing. Do you understand how unit tests work? Each test failure calls the function with a specific value and expects a specific result.

Do you understand which test(s) is failing? What is the exact value the function is being called with and what is the exact value the test is expecting? When debugging tests, you need to look at the exact specific values, not a general “this is what should happen in abstract”.

Sorry, I don’t think I understand what am I suppose to do. :(

This is the failing test:

Do you understand what values this test calls your function with?
Do you understand what this test expects your function to return?
Do you understand what your function actually returns when called with those specific values?

1 Like

Hi @NiluKhodapanah :wave:

While this is not the only thing you should look at in your code, this could be an issue. What type of number (int, float, other) does the above line produce? Does that number work as a list index for all of the test cases?

…and if you add 1 to the length of the list, is that truly the median?

I think for a list which has a length of ( 2n + 1 ), adding 1 (before dividing by 2 to the length of the list should get you the median position. However since lists are 0 indexed you should minus 1 before dividing by 2.

However for test 5 on the alternative averages task, the answer is said to be true however the median is 4 and the average is 5 so surely False is the correct answer.

There are two alternative values to check. Did you check both?

2 Likes

:man_facepalming: I need to stop skim reading the instructions.

I can see where I went wrong.

I have only looked for the median method and not the first/last average method.

Thank you for the hint. :blush:

1 Like