简体   繁体   English

初学者 python 文字游戏,如何从已使用的列表中删除位置

[英]beginner python text game, how do I remove a location from a list that has already been used

How can I get the user to search one area and then be asked if they want to search the others without the already searched location in the list.如何让用户搜索一个区域,然后询问他们是否要在列表中没有已搜索位置的情况下搜索其他区域。 Eg:例如:

where do you want to search? x,y,z
x
where else do you want to search? y,z
search=input('Where do you search first?\nBedside table\nWardrobe\nDesk').lower()
   if 'bedside' in search:
       def bedside():
           pass
       bedside()
   if 'wardrobe' in search:
       def wardrobe():
           pass
       wardrobe()
   if 'desk' in search:
       def desk():
           pass
       desk()

You can maintain a list of locations to search in your code, and simply call list.remove(item) to remove the item once it has been input.您可以维护要在代码中搜索的位置列表,只需调用list.remove(item)即可在输入项目后将其删除。

eg:例如:

locations = [Bedside, Desk, Wardrobe]

search=input(f'Where do you search first? {locations}').lower()

if search in location:
   location.remove(search)

You'll have to track the options somehow.您必须以某种方式跟踪选项。

One approach might be to list out the options in an array, and remove elements from the array as they are selected.一种方法可能是列出数组中的选项,并在选择元素时从数组中删除它们。 See example:参见示例:

options = ['Bedside table', 'Wardrobe', 'Desk']
prompt = "Where would you like to search? "

search = input(prompt + ', '.join(options) + ": ").lower()

if 'bedside' in search:
        #do bedside stuff
        options.remove('Bedside table')

search = input(prompt + ', '.join(options) + ": ").lower()

暂无
暂无

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

相关问题 一旦我的代码已经执行,如何让我的代码循环回输入? (角色扮演游戏) - How do I get my code to loop back to an input once it has been executed already? (RPG Game) Python 初学者 - 如何从列表中召回已删除的项目 - Python Beginner - How do I recall the deleted items from a list 选择并按下按钮后,如何从列表框中删除项目 - How do i remove a item from a Listbox when it has been chosen and a button has been pressed 如何处理已使用Python 2.x从文件中读取的列表中的数据? - How do I manipulate data in a list that has been read in from a file using Python 2.x? 如何使用从文本文件中读取的数据在python中创建字典列表? - how do i create a list of dict in python with the data been read from a text file? 如何确定 PEP 是否已被写入并被拒绝? - How do I find if a PEP has already been written and rejected? 在Mac上,如何在使用sudo pip install openpyxl之前已安装Openpyxl的情况下将其删除? - On a Mac how do I remove Openpyxl when it has already been installed before using sudo pip install openpyxl? 如何将具有两个变量的列表追加到已经具有该列表但具有不同值的另一个列表? 蟒蛇 - how do I append a list with two variables to another list that already has that list in it but with different values? python Python PYPI - 我无法上传我的模块,因为文件名已被使用 - Python PYPI - I cannot upload my module cause the filename has already been used 如果一个值已被使用,我如何输出一个值? - How do i output one value if one has been used?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM