python函数没有返回预期的字符串

时间:2013-08-21 10:25:19

标签: python

我有一个扫描输入文本寻找命令的程序。命令采用大写字母并由<>包围符号。

该程序的一部分包括一个向前扫描下一个命令的功能

def nextCommand(command):
    lar = command.find("<LAR>")
    #...etc (stripped out variable defs used in statements below)
    if lar != -1 and lar > sma or lar > med or lar > bar or lar > fba or lar > fee or lar > lef or lar > rig or lar > cen or lar > end:
        print "nextCommand - lar:" + str(lar)
        return str("<LAR>")
    if sma != -1 and sma > lar or sma > med or sma > bar or sma > fba or sma > fee or sma > lef or sma > rig or sma > cen or sma > end:
        print "nextCommandsma:" + str(sma)
        return str("<SMA>")
        #stripped out more checks
    else:
        return str("")

这在主程序中被调用

    print_text = ""
            if str(nextCommand(input_string)) != "":
                    next_command = str(nextCommand(input_string))
                    print "value recieved from nextCommand() is: " + str(nextCommand)

如果我输入     123 SMA 456 LAR 789 而不是看到SM&gt;我看到了:(取出&lt;符号作为s / o认为它是攻击的一部分) “从nextCommand()收到的值是:

function nextCommand at <0xb74ce1ec>

我做错了什么?

1 个答案:

答案 0 :(得分:4)

您实际上已经打印了该函数的表示。观察:

nextCommand # This references the function

nextCommand() # This calls the function

我认为您打算将next_command放在此处而不是nextCommand

print "value recieved from nextCommand() is: " + str(nextCommand)

应该是:

print "value recieved from nextCommand() is: " + next_command