<p>如何让python3在每次调用它时从列表中随机选择一个项目?
我有两个函数需要处理。我试过物品=随机.randint()和项=随机选择(),但那些人只随机化一次,然后将其存储到item中。在</p>
<p>这里有两个函数。
对于函数1,每次调用它时,我需要该项随机化,这样玩家每次都会得到一个随机项。对于函数2,我需要玩家和老鼠攻击在我选择的数字范围内随机化。所以每当玩家攻击时,它在10到30之间,当老鼠攻击时,它在10到15之间。它必须每转一圈。在</p>
<p>这可能吗?在</p>
<p>功能1。在</p>
<pre><code>def chest(sector):
item = random.choice(items)
print("You see a chest, you unlock it and inside is '{0}'".format(item))
print()
if item in inventory:
print("You already have a {0}".format(item))
item_take = input("Do you wish to take the '{0}'?: ".format(item)).lower()
if item_take == ("yes"):
inventory.append(item)
if item == "Armor":
player["hp"] = 150
print("The {0} has been added to your inventory!".format(item))
sector()
else:
print("You don't take the '{0}'!".format(item))
print()
sector()
</code></pre>
<p>功能2。在</p>
^{2}$