简体   繁体   中英

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

I am completely new to python, so please pardon the terrible arrangement.

I'm trying to create the beginnings of an RPG wherein the player chooses the number of monsters to face, then names the monsters. 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.

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:

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:

# 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.

Also, choosing _ as a variable name is known to mean that it is not actually used anywhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM