Python:将程序中的循环更改为映射函数

Python:将程序中的循环更改为映射函数,python,function,dictionary,Python,Function,Dictionary,首先我必须编写一个程序来计算飞镖游戏的分数,然后我必须将循环更改为map函数。但是我不能把循环转换成映射函数。代码是这样的 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(&quo

首先我必须编写一个程序来计算飞镖游戏的分数,然后我必须将循环更改为map函数。但是我不能把循环转换成映射函数。代码是这样的

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
我做了类似的事情,但它没有给我任何错误或什么,它只是启动程序,然后什么也没有发生,停止程序

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)
def钙芯(x):
打印(“生命点为:”,x)
打印(“中心为:(0,0)”)
距离=数学sqrt((x[0]**2)+(x[1]**2))
打印(“距离为:”,距离)

如@PranavHosangadi所述,如果距离=0,距离=4,距离=8,距离=12,距离=16,距离,则必须迭代映射对象

下面是一个使用列表理解的示例:

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

首先,我认为您需要简洁地重新排列代码

您的代码:

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!")
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)

如果距离=0,距离=4,距离=8,距离=12,距离=16,距离欢迎使用堆栈溢出!请拿着这本书,读一读。当你说“它不起作用”时,你还应该包括你期望发生的事情和实际发生的事情。你有错误吗?您的问题包括完整的堆栈跟踪。您是否获得了意外的输出?在你的问题中包括这一点。在这种情况下,问题似乎是你没有得到任何输出。这是因为使用了一个只在需要时对列表中的每个元素求值的函数。
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)