首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Python中构建具有多个变量和多个选择的随机项?

如何在Python中构建具有多个变量和多个选择的随机项?
EN

Stack Overflow用户
提问于 2013-04-19 07:05:07
回答 1查看 40关注 0票数 0

我应该查看Python文档的哪个区域才能完成以下任务:

代码语言:javascript
复制
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

现在我是confused...basically,我想让程序从已经存储在变量中的一组参数中创建一个字符...

我的方向对吗?提前谢谢。

对Blender的响应:

代码语言:javascript
复制
import random
npc_gender = ["Male","Female"]

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

create_npc()

我这样做了,它帮助我理解。我将继续工作,直到我所有的工作,然后将更新帖子。谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-19 07:07:55

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

代码语言:javascript
复制
import random

...

choice = random.choice(npc_name_male)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16094556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档