如何在Python中使用多个选项构建具有多个变量的随机项?

时间:2013-04-18 23:05:07

标签: python function python-3.x

我应该研究Python文档的哪个领域来完成以下任务:

npc_gender = ["Male","Female"]
npc_age = # trying to do random integer within a range say 18-80
npc_name_male = ["Arnold","Bob","Charles","David","Edward"]
npc_name_female = ["Annie","Barbara","Courtney","Danielle","Ellen"]
""" Obviously, the names are gender specific for an if/else statement :) """
npc_weight = # trying to figure out how to do range...not sure yet, but still variable.
npc_height = # same a weight...not sure yet.

def create_npc():
    if npc_gender == "Male"
        # select (automatically) one of the npc_name_male[#:] and store.
        return npc_name_male[#:]
    elif npc_gender == "Female"
        # select (automatically) one of the npc_name_female[#:] and store.
    else:
        return

现在我感到很困惑......基本上,我希望程序能够从已存储在变量中的一系列参数中创建一个字符...

我是朝着正确的方向前进的吗?在此先感谢。

对Blender的回应:

import random
npc_gender = ["Male","Female"]

def create_npc():
    npc_gender_choice = random.choice(npc_gender)
    print(npc_gender_choice)

create_npc()

我这样做了,这有助于我理解。我将继续努力,直到我完成所有工作,然后将更新帖子。谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用random.choice()获取随机元素:

import random

...

choice = random.choice(npc_name_male)