(Python)关于下面的问题,选项(A)和选项(C)有什么区别?

2024-10-01 07:49:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我在参加在线课程时遇到了这个问题。正确答案是选项(C),但是,为什么我不能选择选项(A)?这两种选择之间有什么细微差别?你知道吗

--->; 假设我们想创建一个类polarbeardunder,一只喝醉的北极熊,它沿着x和y轴随机移动,向南移动时走大步,向北移动时走小步。你知道吗

class PolarBearDrunk(Drunk):
    def takeStep(self):
        # code for takeStep()

以下哪项是takeStep()的适当实现?你知道吗

方案A)

directionList = [(0.0, 1.0), (1.0, 0.0), (-1.0, 0.0), (0.0, -1.0)]
myDirection = random.choice(directionList)
if myDirection[0] == 0.0:
    return myDirection + (0.0, -0.5)
return myDirection

方案B)

directionList = [(0.0, 0.5), (1.0, -0.5), (-1.0, -0.5), (0.0, -1.5)]
return random.choice(directionList)

选项C)

directionList = [(0.0, 0.5), (1.0, 0.0), (-1.0, 0.0), (0.0, -1.5)]
return random.choice(directionList)

选项D)

directionList = [(0.0, 1.0), (1.0, 0.0), (-1.0, 0.0), (0.0, -1.0), (0.0, -1.0)]
return random.choice(directionList)

Tags: 答案gtreturn选项方案randomclass课程