简体   繁体   English

如何从用户输入替换列表中的某个字符串?

[英]How do I replace a certain string in a list from user input?

I made a list which contains a number of names and a menu with options that the user can use to alter the list.我制作了一个列表,其中包含许多名称和一个菜单,其中包含用户可以用来更改列表的选项。 One of the options is to replace one of the names in the list with whatever the user inputs.选项之一是将列表中的名称之一替换为用户输入的任何内容。 How can this be done?如何才能做到这一点? I prefer to not use indexes我更喜欢不使用索引

My code so far:到目前为止我的代码:

print("==================")
names = ["Chris", "Dave", "Joseph", "Harvey", "Levi"]
print(names)
print("==================")

print("(A) Add a name \n(B) Change a name \n(C) Delete a name \n(D) View all names \n(Q) Quit ")
print("==================")

while True:

    choice = input("Select an option: ").lower()

    if choice == "a":
        newName = input("Enter a new name: ")
        names.append(newName)

    elif choice == "b":
        oldName = input("What name would you like to replace?: ")
        newName = input("Enter a new name: ")

        # I don't know what to put here
    

    elif choice == "c":
        delete = input("Which name do you want to remove?: ")
        names.remove(delete)

    elif choice == "d":
        print(names)

    elif choice == "q":
        print("Goodbye")
        break

It really would be a good idea to use index here, and you can look up the specific index at the time the user looks it up.在这里使用索引确实是一个好主意,您可以在用户查找时查找特定索引。

elif choice == "b":
    oldName = input("What name would you like to replace?: ")
    newName = input("Enter a new name: ")

    i = names.index(oldName)
    names[i] = newName

It would also be useful to add validation to make sure that the app doesn't crash when a name is entered that isn't in the list.添加验证以确保在输入不在列表中的名称时应用程序不会崩溃也很有用。 Something like:就像是:

if oldName in names:
    i = names.index(oldName)
    names[i] = newName
else:
    print("That name is not in the list")

The same validation could be added to the delete option.可以将相同的验证添加到删除选项中。

    elif choice == "b":
        oldName = input("What name would you like to replace?: ")
        newName = input("Enter a new name: ")
        names = [x if x != oldName else newName for x in names ]
        print(names)

You can use a list comprehension to add the names if the name does not equal oldName , if the name does equal oldName then add the newName instead.如果名称不等于oldName ,您可以使用列表推导添加名称,如果名称不等于oldName则添加newName

暂无
暂无

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

相关问题 如何将字符串中的一定数量的零替换为要求用户输入的数量,从字符串的末尾开始? - How do I replace a certain amount of zeros in a string with an amount that the user is asked to input, starting from the end of the string? 如何替换 python 列表中字符串中的某些部分? - How do I replace certain pieces in a string in a list in python? 如何用用户输入替换字符串的元素 - How do I replace elements of a string with user input 如何使用discord.py从某些选项列表中可靠地获取用户输入作为变量? - How do I reliably get user input as a variable from a list of certain options using discord.py? 在Python 3.x中,如何替换列表中的某些项目,然后将列表打印为字符串 - In Python 3.x, how do I replace certain items in a list then print list as a string 如何将用户输入与列表中的大写字母字符串进行比较? - How do I compare user input to an uppercase letter string in a list? 如何检查用户输入是否在列表中的随机字符串中? - How can I check if user input is in a random string from a list? 如何根据用户的输入创建列表? 带Python - How do I create a list from the input of a user? w/Python 如何根据用户在 python 中的输入打印列表中的某些项目? - How do i print certain items in a list based on user input in python? 在我的discord机器人键入特定命令后,如何获得所有用户输入的字符串? - How do I get all user input as a string with my discord bot after they type a certain command?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM