小编mas*_*man的帖子

在没有exec/eval的字符串中调用代码,python

我有这个代码,当玩家尝试吃东西时执行:

def eat(target='object'):
    global current_room
    global locations
    global inventory
    if target in inventory:
        items[target]['on_eat'] #This is showing no results.
    else:
        print 'You have no ' + target + ' to eat.'
Run Code Online (Sandbox Code Playgroud)

和这个项目的代码(修剪)

items = {
'strawberry': {
    'weight': 1,
    'text': 'The strawberry is red',
    'on_eat': "normal_eat('strawberry', 'pretty good, but not as sweet as you expected')"
    },
'trees': {
    'weight': 50,
    'text': 'The trees are tall with large, leaf filled branches blocking out a majority of sunlight.',
    'on_eat': "forcesay('Eating trees? What …
Run Code Online (Sandbox Code Playgroud)

python eval reference exec

5
推荐指数
1
解决办法
1705
查看次数

标签 统计

eval ×1

exec ×1

python ×1

reference ×1