不清楚的Python语法/错误

时间:2011-04-28 17:17:56

标签: python eclipse eclipse-plugin pydev

我收到了一个错误:

  

NameError:名称'barley_amount'未定义

这是我的代码:

    from ctypes import *

class barley_amount(Union):
    _fields_ = [
                ("barley_long", c_long),
                ("barley_int", c_int),
                ("barley_char", c_char)
                ]
    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`from ctypes import *

    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

我从一本书中拿了这个例子,甚至在我不断收到错误的时候复制了它。我正在使用PyDev和eclipse。任何人都知道这里发生了什么?哦,Python 2.7.1我也在使用。

2 个答案:

答案 0 :(得分:5)

缩进在Python中很重要。最后一部分(从第13行的ctypes开始)是错误的。真正的代码是:

from ctypes import *

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

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

答案 1 :(得分:0)

我花了很长时间对这个问题猛烈抨击,在工会结束后你没有空白行“”

在值之前添加一个空格,它应该可以在最后再查找已经提到的随机ctypes导入的其他内容。