【问题标题】:How to choose random word in python from a list of words如何从单词列表中选择python中的随机单词
【发布时间】:2018-08-27 18:49:40
【问题描述】:

这是我的代码:

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")

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

【问题讨论】:

  • 您确定North,East,South,West 是一个列表吗? random.choice 需要一个列表作为参数。

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


【解决方案1】:

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

所以你可能会使用:

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

【讨论】:

    【解决方案2】:

    应该这样做(您只需将selectPath 替换为您的输入:

    rightPath = random.choice(['North','East','South','West'])
    selectPath = 'North'
    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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-17
      • 2015-01-26
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      相关资源
      最近更新 更多