初学者Python列表,定义变量

时间:2017-10-20 16:28:59

标签: python-3.x

所以我最初完成这项任务只有一些if,elif语句。但有人要求我将这些陈述的工作分解为单独的职能,作为额外的练习。我需要获取要添加到列表的名称,并能够编辑或删除名称。但我努力让函数将其输出返回到列表中。

这是我到目前为止所拥有的

print("Roster Management")


def addplayer():
    name=input("Enter new player name: ")
    roster = [name]
    list(roster)
    roster.append(name)

def removeplayer():
    name = input('Enter player for removal: ')
    roster.remove(name)


def editplayer():
    oldname = input('Enter name you want to edit: ')
    newname = input('Enter new name: ')
    [x.replace(oldname, newname) for x in roster]


while 1==1:
    print('---------- Main Menu ------------')
    print("Choose from the following options")
    print('1. Display Team Roster')
    print('2. Add Member')
    print('3. Remove Member')
    print('4. Edit Member')
    print('9. Exit Program')
    print(" ")
    selection = input("Enter Selection: ")
    if selection == '1':
        for x in roster:
            print(roster)
    elif selection == '2':
        addplayer()
    elif selection == '3':
        removeplayer()
    elif selection == '4':
        editplayer()
    elif selection == '9':
        print("Closing program...")
        break`enter code here`

2 个答案:

答案 0 :(得分:0)

添加

return list

在定义的最后,因为现在该定义并不知道它应该返回什么。同样在你的if语句中你应该有

print(def()) 

而不仅仅是

def()

希望这有帮助

答案 1 :(得分:0)

您的代码出现了一些问题:

#1

roster = [name] # this creates a new list with a single element in it instead of appending to some existing list
list(roster) # you're trying to convert a list to a list, so not needed
roster.append(name) # you should've some local/global roaster to append to

#2

def removeplayer():
    name = input('Enter player for removal: ')
    roster.remove(name) # again, you should've some local/global roaster to append to
    #in case of local you should return the list for further usage, in case of global you can simply remove

#3

[x.replace(oldname, newname) for x in roster]
# again you're neither updating any existing list/ nor returned anything
# also check if `x==oldname` then replace with new name 

#4

for x in roster:
    print(roster) # x is what you should be printing, not the whole list (roaster)

因此,您使用这些更改的更新代码将是这样的:

roster = [] 
def addplayer():
    name=input("Enter new player name: ")
    roster.append(name)

def removeplayer():
    name = input('Enter player for removal: ')
    roster.remove(name)


def editplayer():
    global roster
    oldname = input('Enter name you want to edit: ')
    newname = input('Enter new name: ')
    roster = [newname if x == oldname else oldname for x in roster]


print("Roster Management")
while 1==1:
    print('---------- Main Menu ------------')
    print("Choose from the following options")
    print('1. Display Team Roster')
    print('2. Add Member')
    print('3. Remove Member')
    print('4. Edit Member')
    print('9. Exit Program')
    print(" ")
    selection = input("Enter Selection: ")
    if selection == '1':
        for x in roster:
            print(x)
    elif selection == '2':
        addplayer()
    elif selection == '3':
        removeplayer()
    elif selection == '4':
        editplayer()
    elif selection == '9':
        print("Closing program...")
        break