首页 > 解决方案 > 如何从单词列表中选择python中的随机单词

问题描述

这是我的代码:

rightPath = random.choice(North,East,South,West)

if selectPath == rightPath:
    print("You slay the monsterous beast and proceed further into the castle")
    print("This is just the start of your dangerous adventure")
else:
    print("The Monster attacks leaving you helpless on the floor")
    time.sleep(4)
    print("You Died")

我想让它从北、东、南、西列表中选择一个随机方向。

标签: pythonpython-3.xpython-2.7

解决方案


random.choice期望一个序列作为参数。

所以你可能会使用:

rightPath = random.choice(['North', 'East', 'South', 'West'])

推荐阅读