Dart excercise pyhton

import math
def score(x, y):
    radius = x ** 2 + y ** 2
    distance = math.sqrt(radius)
    if distance > 10:
        return 0
    elif 5 < distance <= 10:
        return 1
    elif 0 < distance <= 5:
        return 5
    else:
        return 10

I run this code over and over, but there are some tests where the result’s output won’t match the expected result, Is there sth I am not considering here?

Could you share the test results (using a codeblock and not a screenshot)? That would help us (and you!) look at which tests are failing and why.

CODE RUN

self.assertEqual(score(0, -1), 10)

TEST FAILURE

AssertionError: 5 != 10

CODE RUN

self.assertEqual(score(-0.1, -0.1), 10)

TEST FAILURE

AssertionError: 5 != 10

CODE RUN

self.assertEqual(score(0.7, 0.7), 10)

TEST FAILURE

AssertionError: 5 != 10

​​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?

Hint: It seems that the tests that are failing are pretty close to (0, 0)

  1. Well I understand that it tries to calculate the score when the dart is “on the inner circle”, “near the center” and “just within the inner circle”.
  2. I know what the test is expecting but I’m not sure if I know why.
  3. Yes, but the thing I don’t understand which I think questions the way I wrote the code is how the calculated distance can determine the score, I mean I don’t understand what the unit is.

The first test calls your function with values 0, -1 and expects to get back 10. It expects 10 because the location 0, -1 is within the 1 unit inner circle.

The test gets back 5 and complains that it is not the expected value, 10.

What the unit is (cm, inches, meters) doesn’t especially matter.

Does that all make sense?

Yes, I think the thing I don’t understand is how I can determine the range of each circle, I think that’s what I’m having trouble with.

The instructions say,

The outer circle has a radius of 10 units (this is equivalent to the total radius for the entire target), the middle circle a radius of 5 units, and the inner circle a radius of 1.

The distance can be computed using the Pythagorean Theorem which you are doing correctly.

1 Like