简体   繁体   English

如何在 Python 中创建用户定义的列表?

[英]How do I create a user-defined list in Python?

I am completely new to python, so please pardon the terrible arrangement.我对python完全陌生,所以请原谅糟糕的安排。

I'm trying to create the beginnings of an RPG wherein the player chooses the number of monsters to face, then names the monsters.我正在尝试创建 RPG 的开端,其中玩家选择要面对的怪物数量,然后为怪物命名。 The code seems to accept the number input, but when it asks for the monster's names it returns NameError: name 'number' is not defined.代码似乎接受数字输入,但是当它询问怪物的名字时,它返回NameError: name 'number' is not defined.

def welcome():    
    number = monsters()
    print('Alright, ' + str(number) + ' monsters.')
    print('So, what are the names of these beasties?')
    mNames = monsterNames()

def monsters():
    number = input('How many monsters will you face? ')
    return number

def monsterNames():
    totalMobs = [[input('Monster name ')]] * number
    return totalMobs

def main():
    welcome()

main()

What is causing this error and how can I attempt to resolve it?是什么导致了这个错误,我该如何尝试解决它?

You need to send the number of monsters to the function that uses it:您需要将怪物数量发送到使用它的函数:

def welcome():
    number = monsters()
    print('Alright, ' + str(number) + ' monsters.')
    print('So, what are the names of these beasties?')
    # send number of monsters to 'monsterNames'
    mNames = monsterNames(number)

And:和:

# accept number rof monsters as an argument from 'welcome'
def monsterNames(number):
    totalMobs = [input('Monster name ')] * number
    return totalMobs

You should also cast number to an integer as @BhargavRao mentioned:您还应该将number转换为@BhargavRao 提到的整数:

def monsters():
    number = input('How many monsters will you face? ')
    return int(number) # this can fail, use try-catch optimally

Note that I also changed (after comments mentioned it):请注意,我也更改了(在评论提到之后):

[[input('Monster name ')]] * number # creates nested lists (mutable)

To:到:

[input('Monster name ')] * number # creates list of strings (immutable)

Creating a list of mutable objects with list multiplication (using the star operator) is a bad idea.使用列表乘法(使用星号运算符)创建可变对象列表是一个坏主意。 Because you're actually using the same object throughout the list.因为您实际上在整个列表中使用了相同的对象。 If the object is mutable - a change in the object is reflected in the entire list.如果对象是可变的 - 对象的变化会反映在整个列表中。

This is a delicate point for a beginner I suspect, read here for a more detailed explanation.对于我怀疑的初学者来说,这是一个微妙的观点,请阅读此处以获取更详细的解释。 This only works since these are strings, which are immutable, and can not be easily changed.这只有效,因为这些是字符串,它们是不可变的,不能轻易改变。

Now that that's out of the way it's important to note that what you're doing is duplicating the same name over the entire list.既然这已经结束了,重要的是要注意您所做的是在整个列表中复制相同的名称。 What you want to do is create a new name for every monster - so you should use a list comprehension that calls for an input multiple times.想要做的是为每个怪物创建一个新名称 - 因此您应该使用多次调用输入的列表理解。 The new and improved monsterNames function looks like this:新的和改进的monsterNames函数如下所示:

# accept number rof monsters as an argument from 'welcome'
def monsterNames(number):
    totalMobs = [input('Monster name ') for _ in range(number)]
    return totalMobs

This will create a list of monster names by calling input the specified number of times.这将通过调用input指定的次数来创建一个怪物名称列表。

Also, choosing _ as a variable name is known to mean that it is not actually used anywhere.此外,众所周知,选择_作为变量名意味着它实际上并未在任何地方使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 列表创建用户定义的 function? - How to create user-defined function with Python list? 如何在Python中创建图像的KNN图形(使用用户定义的距离度量)? - How do I create KNN graph for images in Python (with user-defined distance metric)? 如何使用用户定义的类实例在python中填充列表? - How do I fill a list in python with user-defined class instances? 如何在 numpy 中累积用户定义的 ufunc? - How do I accumulate a user-defined ufunc in numpy? 如何找到用户定义范围内的所有素数? - How do I find all primes in a user-defined range? 如何在python中的用户定义搜索字符串中支持*? - How can I support * in user-defined search strings in python? Python:从模块创建用户定义方法的列表,并通过用户输入引用/调用它们 - Python: Create list of user-defined methods from a module and referencing/calling them by user input 如何在python中绘制用户定义的分布? - How to draw on a user-defined distribution in python? 如何在 python 中进行用户定义的异常? - How to make user-defined exceptions in python? 如何配置系统,以便Windows Python 2.7 CGI脚本可以看到用户定义的程序包? - How do I configure my system so that Windows Python 2.7 CGI scripts see user-defined packages?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM