【问题标题】:Python: Changing loops in the program into map functionPython:将程序中的循环更改为映射函数
【发布时间】: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 Askquestion checklist。当您说“它不起作用”时,您还应该包括您预期会发生的事情以及实际发生的事情。你收到错误了吗? Edit 你的问题包括完整的堆栈跟踪。你有没有得到意想不到的输出?将其包含在您的问题中。
  • 在这种情况下,问题似乎是你得到没有输出。这是因为map() 使用了generator,它只在需要时评估列表中的每个元素。

标签: python function dictionary


【解决方案1】:

正如@PranavHosangadi 所说,您必须遍历映射的对象。

这是一个使用列表推导的示例:

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

【讨论】:

    【解决方案2】:

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

    您的代码:

    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 函数是一个交互器。如果您需要取出所有项目,则必须使用循环。

    【讨论】:

      猜你喜欢
      • 2015-08-24
      • 1970-01-01
      • 2014-04-05
      • 2021-01-22
      • 1970-01-01
      • 2018-10-22
      • 2021-12-02
      • 2018-01-18
      • 1970-01-01
      相关资源
      最近更新 更多