Python: Changing loops in the program into map function

Viewed 43

First I had to write a program to calculate dart game scores, then I had to change the loops into map function. But I can't change loops into map function. The code is this;

import math
hitpoints=[(7,5), (2,6), (1,-1), (-3,-9), (-7,16), (2,-2), (6,1), (4,4), (9,6), (7,-4)]

i=0
while i < 10:
    print("Hit point is: ", hitpoints[i])
    print("Center is: (0,0)")
    distance=math.sqrt((hitpoints[i][0]**2)+(hitpoints[i][1]**2))
    print("The distance is: ", distance)


    if distance <= 19:
        print("Result: True")
        print("Hit the board!")
    
        if distance >= 0 and distance <= 3:
            print("Score: 10")
        if distance >= 4 and distance <= 7:
            print("Score: 5")
        if distance >= 8 and distance <= 11:
            print("Score: 3")
        if distance >= 12 and distance <= 15:
            print("Score: 2")
        if distance >= 16 and distance <= 19:
            print("Score: 1")
    else:
        print("Outside of the board!")
    print("---------------------------------------------")
    i=i+1

Here is the output of this code

Hit point is:  (7, 5)
Center is: (0,0)
The distance is:  8.602325267042627
Result: True
Hit the board!
Score: 3
---------------------------------------------
Hit point is:  (2, 6)
Center is: (0,0)
The distance is:  6.324555320336759
Result: True
Hit the board!
Score: 5
---------------------------------------------
Hit point is:  (1, -1)
Center is: (0,0)
The distance is:  1.4142135623730951
Result: True
Hit the board!
Score: 10
---------------------------------------------
Hit point is:  (-3, -9)
Center is: (0,0)
The distance is:  9.486832980505138
Result: True
Hit the board!
Score: 3
---------------------------------------------
Hit point is:  (-7, 16)
Center is: (0,0)
The distance is:  17.46424919657298
Result: True
Hit the board!
Score: 1
---------------------------------------------
Hit point is:  (2, -2)
Center is: (0,0)
The distance is:  2.8284271247461903
Result: True
Hit the board!
Score: 10
---------------------------------------------
Hit point is:  (6, 1)
Center is: (0,0)
The distance is:  6.082762530298219
Result: True
Hit the board!
Score: 5
---------------------------------------------
Hit point is:  (4, 4)
Center is: (0,0)
The distance is:  5.656854249492381
Result: True
Hit the board!
Score: 5
---------------------------------------------
Hit point is:  (9, 6)
Center is: (0,0)
The distance is:  10.816653826391969
Result: True
Hit the board!
Score: 3
---------------------------------------------
Hit point is:  (7, -4)
Center is: (0,0)
The distance is:  8.06225774829855
Result: True
Hit the board!
Score: 3
---------------------------------------------

I did something like this but it didnt give me any error or something, it just starts the program and nothing happens then stops the program.

def calcScore(x):
    print("Hit point is: ", x)
    print("Center is: (0,0)")
    distance=math.sqrt((x[0]**2)+(x[1]**2))
    print("The distance is: ", distance)
    
    
    if distance <= 19:
        print("Result: True")
        print("Hit the board!")
        
        if distance >= 0 and distance <= 3:
            print("Score: 10")
        if distance >= 4 and distance <= 7:
            print("Score: 5")
        if distance >= 8 and distance <= 11:
            print("Score: 3")
        if distance >= 12 and distance <= 15:
            print("Score: 2")
        if distance >= 16 and distance <= 19:
            print("Score: 1")
    else:
        print("Outside of the board!")
    print("---------------------------------------------")

map(calcScore, hitpoints)

How can I write this with map function? Any help will be appreciated

2 Answers

As @PranavHosangadi stated, you'll have to iterate over the mapped object.

Here's an example using list comprehension:

mapped_result = map(calcScore, hitpoints)
[x for x in mapped_result]

First of all, I think you need to rearrange your code concisely.

Your code:

if distance <= 19:
    print("Result: True")
    print("Hit the board!")
    
    if distance >= 0 and distance <= 3:
        print("Score: 10")
    if distance >= 4 and distance <= 7:
        print("Score: 5")
    if distance >= 8 and distance <= 11:
        print("Score: 3")
    if distance >= 12 and distance <= 15:
        print("Score: 2")
    if distance >= 16 and distance <= 19:
        print("Score: 1")
else:
    print("Outside of the board!")

My rearrange:

def calcScore(x):
    print("Hit point is: ", x)
    print("Center is: (0,0)")
    distance = ((x[0]**2)+(x[1]**2))**0.5 # Change here, dont need to import math module
    print("The distance is: ", distance)
    
    if distance <= 19:
        print("Result: True")
        print("Hit the board!")
        
        if 0 <= distance <= 3: # Change from here
            print("Score: 10")
        if 4 <= distance <= 7:
            print("Score: 5")
        if 8 <= distance <= 11:
            print("Score: 3")
        if 12 <= distance <= 15:
            print("Score: 2")
        if 16 <= distance <= 19: # To here
            print("Score: 1")
    else:
        print("Outside of the board!")
    print("---------------------------------------------")

hitpoints=[(7,5), (2,6), (1,-1), (-3,-9), (-7,16), (2,-2), (6,1), (4,4), (9,6), (7,-4)]

for x in map(calcScore, hitpoints):
    print(x)

Finally, map function is a interator. If you need to take out all items of this you have to use a loop.

Related