如何从单词列表中选择python中的随机单词

时间:2018-08-27 18:49:41

标签: python python-3.x python-2.7

这是我的代码:

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

if selectPath == rightPath:
    print("You slay the monsterous beast and procceed 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")

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

2 个答案:

答案 0 :(得分:3)

random.choice需要一个序列作为参数。

所以您可能会使用:

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

答案 1 :(得分:1)

这应该做到(您只需用输入内容替换selectPath

rightPath = random.choice(['North','East','South','West'])
selectPath = 'North'
if selectPath == rightPath:
    print("You slay the monsterous beast and procceed 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")