2010-02-27 189 views
0

這裏是我的代碼的摘錄:參數沒有得到正確傳遞

def listFrom(here): 
    print "[DBG] here: " + here 

def book(here, there, amount): 
    print "[DBG] here: " + here + "; there: " + there + "; amount: " + str(amount) 

# Code that takes input and stores it into the string input 

# Yes, I know this is dangerous, but it's part of a 
# school assignment where we HAVE to use eval. 
eval(input, {"__builtins__": {}, "listAll": listAll, "listFrom": listFrom, "listFromTo": listFromTo, "book": book, "about": about, "commands": commands, "book": book}) 

如果我進入listFrom('LON'),按預期程序返回[DBG] here: LON。但是,當我做book('LON', 'MAN', 8)時,我收到了一個莫名其妙的[DBG] here: ☺; there: ☻; amount: ♥。這可能是什麼原因?

+0

你在哪裏看輸出?你可以發佈IDLE會話嗎?否則無法複製 – SilentGhost 2010-02-27 12:43:28

+0

無法複製。什麼Python和哪個平臺? – 2010-02-27 12:43:50

+0

它似乎對我來說工作正常;你可以發佈你用來獲取用戶輸入的代碼嗎?另外,我會調用字符串變量而不是'input',這是內置函數之一。 您使用的是什麼版本的Python? – tzaman 2010-02-27 12:57:58

回答

0

此代碼的工作沒有任何問題在Python 2.6在Linux/X86-32:

>>> def listFrom(here): 
...  print "[DBG] here: " + here 
... 
>>> def book(here, there, amount): 
...  print "[DBG] here: " + here + "; there: " + there + "; amount: " + str(amount) 
... 
>>> book('LON', 'MAN', 8) 
[DBG] here: LON; there: MAN; amount: 8 
>>> input = """book('LON', 'MAN', 8)""" 
>>> eval(input, {"__builtins__": {}, "listFrom": listFrom, "book": book}) 
[DBG] here: LON; there: MAN; amount: 8 
>>> eval("""listFrom('LON')""", {"__builtins__": {}, "listFrom": listFrom, "book": book}) 
[DBG] here: LON 

什麼Python版本您使用的?在哪個OS /體系結構上?

+0

哦拍,你說得對。我隔離的代碼工作正常,但我做了一些古怪的東西,導致'eval'在某些情況下根本不被執行。 – Pieter 2010-02-27 13:26:53