string conversion and formatters
Chris Liechti
cliechti_spam at gmx.net
Fri Dec 7 15:12:20 EST 2001
[posted and mailed]
"Spiffy" <clams17 at yahoo.com> wrote in
news:oZ8Q7.253287$W8.9366851 at bgtnsc04-news.ops.worldnet.att.net:
> 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?
here: http://python.org/doc/2.1.1/lib/typesseq-strings.html
>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"
ever tried long strings?: """blah blah""" with this you could simply write
one print and the string over multiple lines.
> 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)
^^^^^
these parentheses don't do anything useful here. if you want a single
element tuple use "(hex,)". note also that your variable "hex" shadows the
built in function "hex". i would sugest a difrent name.
> TypeError: an integer is required
> Why doesn't this work?
raw_input returns a string. convert it with int(s,16)
> 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
>
>
--
Chris <cliechti at gmx.net>
More information about the Python-list
mailing list