Mac OS X Yosemite Python联合输出错误

2024-04-20 09:46:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我不确定在这里问这个问题是否合适,因为我在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

据塞茨先生说,这个脚本的输出是

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年写的,他的例子只包括windows和linux,有什么好的python入门教程吗?对于mac用户?你知道吗

非常感谢 吉姆


Tags: ofthetoputmyasamountlong
1条回答
网友
1楼 · 发布于 2024-04-20 09:46:30

这是我发现的

  1. value=...后面的部分可能应该是不缩进的。目前,它认为代码应该在类本身中,所以它在初始化类时运行它,并给出一个错误,说明未定义代码量。尽管在您将代码粘贴到这里时可能就是这样。

  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键):

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是否允许您与命令行交互。你知道吗

相关问题 更多 >