[Tutor] Re: number conversion problem (was: Re: [no subject])

Dragonfirebane at aol.com Dragonfirebane at aol.com
Tue Jun 8 17:37:41 EDT 2004

In a message dated 6/8/2004 5:17:15 PM Eastern Daylight Time, lsloan-000002 at umich.edu writes:

> --On Tuesday, June 8, 2004 4:45 PM -0400 Dragonfirebane at aol.com wrote:
> > the full code as is is below (not working). as of right now, i'm
> > oncentrating only on the text function since the number converter works
> > fine. the reason i'm using if and not for is that using for creates a
> > loop where the program prints the converted number based on the number of
> > characters in the original number (1100                  1100) or
> > something of the sort for 12, whereas 'if' prints the same number (1100)
> > once, due to the [print binary[int(original) - 1]] command.
> OK.  Then I think we're back to the problem being that you are referencing 
> "char" before you ever assign any value to it.  The error I get is:
>      Please enter numbers or text to be converted. 1234
>      Traceback (most recent call last):
>        File "multivert.py", line 33, in ?
>          if char in number:
>      NameError: name 'char' is not defined
> What do you expect the variable "char" to contain?  A single character of 
> the user's input string, one character at a time until you reach the end of 
> the string?
> If so, you're going to need a line like this somewhere just before you 
> reference "char":
>      for char in original:
> I've not checked this, but it would probably go just before your "if char 
> in number" statement.

thanks, that looks like it might work.
> Also, I don't know the details of the problem you're trying to solve, but 
> there are more mathematical ways of converting numbers from one base to 
> another that would not require you to include long lists of 
> all the 
> possible conversions in your program.

basically, i'm writing a program to convert text into binary into hexadecimal in all the possible combinations of those (text - bin, text - hex, bin - text, bin - hex, hex - bin, hex - text).  while more mathematical possibilities no doubt exist, i'm not up to delving into 'math' right at this moment.  also, i wanted to make it work without importing too many things.

"n thats the way the cookie crumbles"

America's Favorite Cookie

More information about the Tutor mailing list