[英]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.