首先我必须编写一个计算飞镖游戏分数的程序,然后我必须将循环转换为地图函数。但是我不能把循环变成映射函数。代码如下;
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
下面是这段代码的输出
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
---------------------------------------------
我做了这样的事情,但是它没有给我任何错误或什么,它只是启动程序,什么也没有发生,然后停止程序。
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)
我怎么用map函数写这个?如有任何帮助,不胜感激
正如@PranavHosangadi所说,您必须遍历映射对象。
下面是使用列表推导式的例子:
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)
最后,map函数是一个交互器。如果你需要取出其中的所有项,你必须使用循环。