python :函数

标签 python

我正在尝试为类创建一个基于文本的冒险游戏,但在使用库存中的元素时,我在使用“使用”功能时遇到了问题。

游戏有 2 个交互式菜单,主菜单(您可以在其中旅行、搜索、显示和使用库存、显示 ASCII map 或退出)和旅行菜单(一个让您在不同地点之间移动的 while 循环)。我想将一扇门锁在某个位置,要求用户在不同的位置找到一把 key (从而将其添加到库存中),然后在提示访问该位置时从库存中选择该项目。

我创建了一个字典供 use 函数引用,以验证该项目是否可以在特定位置使用。我测试了函数内的逻辑并且它有效。但是,由于某种原因,它会接受在门上使用的任何元素,我认为这与功能相互处理的方式有关,因为显示库存功能调用了使用功能。

无论是具体问题还是您可能采取不同做法的任何帮助,我们都将不胜感激。

这些是有问题的函数:

def eHouseAccess(action, location, oldLocation): # called by travel to validate if the location is accessable
    global eHouse
    if eHouse == 'locked' and inventory == []:
        print "The door is locked! You need to find a key for this door."
        travel(oldLocation)
    elif eHouse == 'locked':
        print "The door is locked! You need to find a key for this door."
        print "Maybe you have it in your inventory?"
        a = showInv(inventory, location, items)
        if a == True:
            eHouse = 'open'
            travel(location)
        else:
            travel(oldLocation)
    else:
        location = travelOpt[location][action - 1]
        travel(location)


def travel(location):
    while True:
        print "You are in the", location[0]+"." 
        print location[1]
        print 'You can travel to:'

        for (i, t) in enumerate(travelOpt[location]):
            print i + 1, t[0]

        action = raw_input("Pick a destination, or enter 'menu' for the main menu: ")
        if action == 'menu':
            main_menu(location, inventory, items)
        else:
            action = int(action)      
        if travelOpt[location][action - 1] == engineer_house:
            oldLocation = location
            location = engineer_house
            eAccess = eHouseAccess(action, location, oldLocation)
        elif travelOpt[location][action - 1] == castle_inside:
            cInside = cInsideAccess(action, location, cInside)
        else:
            location = travelOpt[location][action - 1]


def main_menu(location, inventory, items):
    print "You are in the", location[0] + "."
    menu_list = ['Travel', 'Inventory', 'Search', 'Map', 'Quit']
    print "Choose one:"
    for (num, t) in enumerate(menu_list):
        print num + 1, t
    main_choice = int(raw_input("> "))
    action = menu_list[main_choice - 1]
    if action == 'Travel':
        travel(location)
    elif action == 'Inventory':
        showInv(inventory, location, items)
    elif action == 'Search':
        search(location, inventory, items)
    elif action == 'Map':
        map(location)
    elif action == 'Quit':
        exit()
    else:
        print "That is not a valid option!"
        main_menu(location, inventory, items)


def showInv(inventory, location, items):
    if inventory == []:
        print "Your inventory is EMPTY"
        sInv = raw_input("Hit 'enter' to return to the 'main menu': ")
        main_menu(location, inventory, items)
    else:
        print "These 'items' are in your 'inventory':"
        for (num, i) in enumerate(inventory):
            print num + 1, i
    sInv = raw_input("Type 'menu' to return to the main menu or 'use' to use and item: ")
    if sInv == 'menu':
        main_menu(location, inventory, items)
    if sInv == 'use':
        a = use(items, inventory, location)
        return a
    else:
        print "That is not a valid entry!"
        showInv(inventory, location, items)


def use(items, inventory, location):
    if inventory == []:
        print "There is nothing to use."
        invEmpty = raw_input("Hit 'enter' to return to the 'main menu': ")
        main_menu(location, inventory, items)
    else:
        uItem = int(raw_input("Choose an item to use: "))
        curItem = inventory[uItem - 1]
    if location == items[curItem][0]:
        print "You used", inventory[uItem - 1]+"."
        inventory.pop(uItem -1)
        main_menu(location, inventory, items)
        return True
    else:
        print "You cannot use that here!"
        main_menu(location, inventory, items)
        return False

最佳答案

有两个问题对我来说很突出。首先,use(...) 返回一个 bool 值,而不是使用的项目。无论使用的项目是什么,use(...) 都会返回 True。其次,在 eHouseAction 方法中,您正在测试 showInv(...) 返回的值是否等于 True。

由于 use(...) 返回 True 并且 showInv(...) 返回 use(...),因此 a = showInv(...) 将被设置为 True。 eHouseAction 正在检查 True 以打开门。由于使用库存中的任何元素都会导致 showInv(...) 返回 True,因此使用库存中的任何元素都会打开大门。

解决方案是进行两项更改:

def eHouseAccess(action, location, oldLocation): 
    [snip]
    a = showInv(inventory, location, items)
    if a == house_key: # I'm not exactly sure what you called the key
        eHouse = 'open'
        travel(location)

def use(items, inventory, location):
    [snip]
    if location == items[curItem][0]:
        print "You used", inventory[uItem - 1]+"."
        return inventory.pop(uItem -1)

现在,如果玩家不尝试使用 house_key,那么将用过的元素放回玩家的库存中可能是个好主意。如果没有这项检查,他们使用的任何元素都将永远消失。

关于 python :函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8005188/

相关文章:

python - 在 Python 中退出 while 循环

python - 如何用正则表达式匹配一个不以 X 开头但以 Y 结尾的单词

python - 从具有非数字索引的数据框中删除行

python - 为什么 `max` 和 `min` 在使用空可迭代对象调用时不返回适当的无穷大?

python - 使用 pandas read_csv 时出现 urllib.error.URLError

python - 如果列表项以 '.' 开头,为什么 python3 不从列表创建文件夹?

python - 带有变量赋值的python中的列表理解

python - Mongoengine Transactions怎么来的?

python - 使用 Python 抓取时丢失数据?

Python any(iterable) 和 all(iterable) 没有短路和副作用