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