0

No sure if appropriate to ask this questions here, since I am a complete noob in Python. I am following the instruction on Gray Hat Python and was working on union coding.

this is the original code

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

according to Mr. Seitz, the output of this script would be

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

and this is what my results from Eclipse looks like

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

can anyone tell me where I did wrong, or what am I missing? Mr. Seitz's book was written in 2009, and his examples only include window and linux, is there any good intro to python for beginner? for mac users?

Very much appreciate Jim

Jim
  • 3
  • 1

1 Answers1

1

Here is what I found

  1. The part after value=... should probably be un-indented. Currently it thinks that code should be in the class itself, so it's running it as the class is being initialized and giving an error saying barley_amount is not defined. Although that might have just been the case as you were pasting the code into here.

  2. I think you might not need the 66 part in the raw_input. That's the part you are supposed to enter. Like so:

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

Here is what I get (I typed in the 64 and hit 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: @

Not sure if eclipse lets you interact with the command line.

  • not sure if I'm doing correctly, i tried to enter 64 in the console and press enter, there is this one comment: NameError: name 'barley_amount' is not defined ERROR: Module: chapter1-unions could not be imported (file: /Users/Picarolife/Documents/workspace/Gray hat Python/chapter1-unions.py). done.unions.py). done. – Jim Jan 24 '15 at 19:28
  • ok thank you so much, i am still not sure exactly where i did wrong, but now it work just fine after i tried copy and paste your code. Thanks again for your time and help! – Jim Jan 26 '15 at 17:38
  • @Jim no problem - but were you considering marking this as the correct answer or anything? :) – Mali Akmanalp Jan 29 '15 at 19:31