每次调用时,如何让Python 3从列表中选择一个随机项? 我有两个功能,我需要这样做。我已经尝试过item = random.randint()和item = random.choice(),但那些只将它随机化一次然后将其存储到item中。
这是两个功能。 使用功能1,我每次调用它时都需要随机化项目,因此玩家每次都会得到一个随机项目。对于功能2,我需要玩家和老鼠的攻击在我选择的数字内随机化。所以每当玩家攻击时,它就会在10到30之间,当老鼠攻击它时,它会在10到15之间。每回合都必须这样做。
这可能吗?
功能1.
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()
功能2。
player = dict(
name = " ",
att = random.randint(10, 30),
hp = 100,)
rat = dict(
name = "Rat",
att = random.randint(10, 15),
hp = 20,)
def attack(player, enemy):
firstAtt = random.randint(1, 2)#Player = 1, Enemy = 2. Checks to see who goes first.
if firstAtt == 1:
while player["hp"] > 0 and enemy["hp"] > 0:
enemy["hp"] = enemy["hp"] - player["att"]
print("You have dealt {0} damage to the {1}!".format(player["att"], enemy["name"]))
if enemy["hp"] <= 0:
print()
print("You have killed the {0}".format(enemy["name"]))
print("You have {0}HP left.".format(player["hp"]))
break
player["hp"] = player["hp"] - enemy["att"]
print("The {0} has dealt {1} damage to you!".format(enemy["name"], enemy["att"]))
if player["hp"] <= 0:
print()
print("The {0} has killed you!".format(enemy["name"]))
break
elif firstAtt == 2:
while player["hp"] > 0 and enemy["hp"] > 0:
player["hp"] = player["hp"] - enemy["att"]
print("The {0} has dealt {1} damage to you!".format(enemy["name"], enemy["att"]))
if player["hp"] <= 0:
print()
print("The {0} has killed you!".format(enemy["name"]))
break
enemy["hp"] = enemy["hp"] - player["att"]
print("You have dealt {0} damage to the {1}".format(player["att"], enemy["name"]))
if enemy["hp"] <= 0:
print()
print("You have killed the {0}".format(enemy["name"]))
print("You have {0}HP left.".format(player["hp"]))
break
答案 0 :(得分:2)
chest()
没问题。
对于attack()
,将att
元素转换为函数,然后根据需要调用它。
att=lambda: random.randint(...),
...
dmg = player['att']()
...
答案 1 :(得分:1)
除非你试图完全没有类别(也许是作为课堂作业或理解函数式编程或词典的练习),否则python类会大量清理你的代码。这也有很多好处。例如,在此代码中,我可以轻松访问最小和最大攻击值,可能显示在板上或攻击对话框中的角色令牌上。这是一个准系统示例,您需要对其进行扩展并进一步修改以满足您的需求。就个人而言,我会加入一个“take_damage”函数来处理攻击的伤害(这样,我可以减少atk_min或atk_max作为我想要的伤害的函数或者在他们自己的函数中做其他与伤害相关的效果)和每个变量,我都有另一个“当前”变量(self.current_atk_min,self.current_atk_max,self.current_hp)等。这样,我可以独立于当前变量跟踪原始值并应用项目奖金,魔法奖金,伤害处罚等,不会失去原有价值。
from random import randint
class Character():
def __init__(self, name, atk_min, atk_max, hp):
self.name = name
self.atk_min = atk_min
self.atk_max = atk_max
self.hp = hp
def attack(self):
return randint(self.atk_min, self.atk_max)
rat = Character('rat', 10, 15, 20)
player = Character('Player', 15, 30, 100)
我知道代码更长,但它更容易被发现,未来你会感谢Current You 2年后(从我让坐下来的问题写了近2,000行kludgey代码的人那里拿走它两年没有看它。我不得不全力以赴,因为我再也无法说出它应该做得多好以便能够聪明地改变它。)