0

每次调用时,如何让 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
4

2 回答 2

2

chest()很好。

对于attack(),将att元素转为函数,然后酌情调用。

  att=lambda: random.randint(...),

 ...

      dmg = player['att']()
       ...
于 2012-08-20T00:46:01.317 回答
1

除非您尝试完全无类(可能作为课堂作业或作为理解函数式编程或字典的练习),否则这是 Python 类大量清理您的代码的情况。这也有许多连锁反应。例如,在这段代码中,我可以轻松访问最小和最大攻击值,可能会显示在棋盘上的角色标记或攻击对话框中。这是一个准系统示例,您需要对其进行扩展并进一步修改以满足您的需要。就个人而言,我会包含一个“take_damage”函数来处理攻击造成的伤害(这样,如果我想要或在它们自己的函数中执行其他与伤害相关的效果,我可以根据所承受的伤害减少 atk_min 或 atk_max)和每个变量,我都会有另一个“当前”

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)

我知道代码更长,但更容易发现,Future You 会感谢 Current You 2 年后(从一个我让他坐了 2 年的问题上写了近 2,000 行杂乱代码的人那里得到它没有看它。我不得不把它全部扔掉,因为我不再知道它应该做得足够好来智能地改变它)。

于 2012-08-20T08:16:45.210 回答