Skizz
Skizz

Reputation: 1465

Unclear Python Syntax/Errors

I'm getting an error:

NameError: name 'barley_amount' is not defined

Here's my code:

    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

I took this example from a book, and even copy posted it when I kept getting errors. I'm using PyDev with eclipse. Anyone have any idea on what's going on here? Oh, Python 2.7.1 I'm using, too.

Upvotes: 0

Views: 428

Answers (2)

user3225329
user3225329

Reputation: 1

i spent a long time banging my head on this as well, you do not have a blank line after the end of the union "]"

add a space before value and it should work find other then the already mentioned random ctypes import at the end again.

Upvotes: 0

utdemir
utdemir

Reputation: 27216

Indentation matters in Python. And the last part(starts with `from ctypes at line 13) is wrong. The true code is:

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

Upvotes: 5

Related Questions