首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python:函数

python:函数
EN

Stack Overflow用户
提问于 2011-11-04 13:19:24
回答 2查看 341关注 0票数 2

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

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

我创建了一个字典,以供use函数引用,以验证该项是否可以在特定位置使用。我测试了函数中的逻辑,它起作用了。但是,由于某些原因,它将接受门上使用的任何项目,我认为这与函数之间的处理方式有关,因为use函数是由show清单函数调用的。

在这里,无论是对特定问题还是您可能采取不同方式的任何事情,任何帮助都将不胜感激。

以下是有问题的函数:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-04 13:40:43

嗯,我甚至不确定eHouseAccess是如何工作的--你应该使用NameError: global name 'items' is not defined。你可能想要,global eHouse, items。你的bug,我猜,一定和engineer_house.有关您正在尝试将其与items[curItem][0]进行比较。你的设置正确吗?

其他注释:

use (这不是最好的名称)内部,您可能希望在第一个else子句之前有一条return语句。

我也会在代码评审中指出这是一个问题:

代码语言:javascript
复制
if location == items[curItem][0]:

为什么它有一个0索引?似乎将某种数据对象放在那里会更有意义。然后,代码可能如下所示:

代码语言:javascript
复制
if location == items[curItem].location:

或者更好的做法是,使该位置属性成为可以使用它的位置的列表,并且您可以拥有:

代码语言:javascript
复制
if location in items[curItem].valid_locations:

当然,我仍然会返回选定的对象,以及它是否可以使用。否则,在你有两件或更多事情可以做的情况下,你可能会不小心用肥皂刷牙。

票数 2
EN

Stack Overflow用户

发布于 2011-11-04 13:39:33

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

由于使用(...)返回True和showInv(...)返回use(...),然后返回a= showInv(...)设置为True。eHouseAction正在检查True以打开门。因为使用库存中的任何项目都会导致showInv(...)返回True,使用库存中的任何物品都会打开门。

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

代码语言:javascript
复制
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,那么将用过的物品放回玩家的库存中可能是一个好主意。没有这张支票,他们使用的任何物品都将永远消失。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8005188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档