string conversion and formatters

Spiffy clams17 at yahoo.com
Fri Dec 7 14:43:16 EST 2001


Hello! I am  total newbie to programming and Python. I am teaching myself
with online tutes and documentaion. First, does anyone know where the list
of  output formatters ( such as %d, %s, etc.) is in the reference material?
I know I have seen this list, but I cannot find it.
By the way, I'm using Python2.2 on Windows 98.
Secondly, here is my first real program....
#Decimal/Hex Convertor

def print_options():
    print "Options:"
    print " 'a' decimal to hex"
    print " 'b' hex to decimal"
    print " 'c' print options"
    print " 'd' quit"

choice = "c"
while choice != "d":
    if choice == "a":
        dec = input("Decimal: ")
        print "Hex: %X" % (dec)
    elif choice == "b":
        hex = raw_input("Hex: ")
        print "Decimal: %d" % (hex)
    elif choice != "d":
        print_options()
    choice = raw_input("option: ")

Please dont laugh. It is supposed to convert decimals to hex numbers and
vice-versa. It works fine until you try to convert hex to decimal and then
here's what I get....
Options:
 'a' decimal to hex
 'b' hex to decimal
 'c' print options
 'd' quit
option: b
Hex: A
Traceback (most recent call last):
  File "C:\Program Files\Python2\convertor.py", line 17, in ?
    print "Decimal: %d" % (hex)
TypeError: an integer is required
Why doesn't this work? I have tried using "string.atoi" and some other
things, but no luck. I am stumped. Please, if you have an explanation or any
hints to point me in the right direction, I would very much appreciate it.
...just dont laugh at me...I'm a newbie!
Thanks,
  Spiffy





More information about the Python-list mailing list