【发布时间】:2021-01-15 14:59:38
【问题描述】:
首先我必须编写一个程序来计算飞镖游戏的分数,然后我必须将循环更改为 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
这是这段代码的输出
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)
我怎样才能用地图功能写这个?任何帮助将不胜感激
【问题讨论】:
-
欢迎来到 Stack Overflow!请使用tour,并阅读How to Ask 和question checklist。当您说“它不起作用”时,您还应该包括您预期会发生的事情以及实际发生的事情。你收到错误了吗? Edit 你的问题包括完整的堆栈跟踪。你有没有得到意想不到的输出?将其包含在您的问题中。
标签: python function dictionary