Mac OS X Yosemite Python Eclipse 上的联合输出错误

Mac OS X Yosemite Python union output error on Eclipse

不确定在这里问这个问题是否合适,因为我是 Python 中的菜鸟。我正在按照 Gray Hat Python 上的说明进行联合编码。

这是原码

from ctypes import *

class barley_amount(Union):
    _fields_ = [
                ("barley_long", c_long),
                ("barley_int", c_int),
                ("barley_char", c_char * 8),
                ]

    value = raw_input("Enter the amount of barley to put into the beer vat: 66")
    my_barley = barley_amount(int(value))
    print "Barley amount as a long: %ld" % my_barley.barley_long
    print "Barley amount as an int: %d" % my_barley.barley_long
    print "Barley amount as a char: %s" % my_barley.barley_char

根据 Seitz 先生的说法,此脚本的输出将是

Enter the amount of barley to put into the beer vat: 66 

        Barley amount as a long: 66    
        Barley amount as an int: 66
        Barley amount as a char: B

这就是我从 Eclipse 中得到的结果

Finding files... done.
Importing test modules ... Enter the amount of barley to put into the beer vat: 66

谁能告诉我哪里做错了,或者我错过了什么? Seitz先生的书是2009年写的,他的例子只有window和linux,请问有没有适合初学者的python介绍? mac 用户?

非常感谢 吉姆

这是我找到的

  1. value=... 之后的部分可能应该取消缩进。目前它认为代码应该在 class 本身,所以它是 运行 因为 class 正在初始化并给出一个错误说 barley_amount 未定义。尽管当您将代码粘贴到此处时可能就是这种情况。

  2. 我认为您可能不需要 raw_input 中的 66 部分。那是 应该输入的部分。像这样:

from ctypes import *

class barley_amount(Union):
    _fields_ = [
                ("barley_long", c_long),
                ("barley_int", c_int),
                ("barley_char", c_char * 8),
                ]

value = raw_input("Enter the amount of barley to put into the beer vat:")
my_barley = barley_amount(int(value))
print "Barley amount as a long: %ld" % my_barley.barley_long
print "Barley amount as an int: %d" % my_barley.barley_long
print "Barley amount as a char: %s" % my_barley.barley_char

这是我得到的(我输入 64 并按回车键):

Enter the amount of barley to put into the beer vat: 64
Barley amount as a long: 64
Barley amount as an int: 64
Barley amount as a char: @

不确定 eclipse 是否允许您与命令行交互。