[Tutor] strange behavior in program

Dragonfirebane at aol.com Dragonfirebane at aol.com
Sat Jun 12 18:06:58 EDT 2004


>Hi Dragonfirebane!

This (to you) unexpected behaviour comes from the fact, that in


   for char in original:
       try:
           int(original)
       except TypeError:
           convertxt()
       except ValueError:
           convertxt()
       else:
           if char in number:
               convertnum()
       again = raw_input("Would you like to convert more text or numbers [y/n]? ")
       if again in 'Yy':
           again = True
       elif again in 'Nn':
           again = False

the raw_input() call is made once for every char in original,
that is - in your example - in "hello": 5 times!
And only if you answer with n in the last of these five inputs
the enclosing while loop will terminate.

So, I think, you shouldn't put the last five lines into the
for-loop.

Apart from this there are several flaws in your program, which
make it hard for me to understand, what it should do.
And because the program seems to be relatively complex it's
also hard to tear them apart and isolate them.

Two tiny examples:

(1)I'd much prefer if you wrote something like:
   more = raw_input("Would you like to convert more text or numbers [y/n]? ")
   if more in 'Yy':
       again = True # albeit this doesn't change anything, because
                    # if again weren't true you wouldn't get here
   elif  more in "Nn":
       again = False

   (I mean: why use the same name for different things?)

(2) The inner "while loop" in convertxt is a strange construction
   because it contains an unconditional break, which (unconditionally)
   terminates the loop after the first run through it.



       while char in alphabet[x]:
           print binary[x]
           break

   I think this should equivavlently better be written as:

       if char in alphabet[x]:
           print binary[x]

   (Moreover note, on the other side, that without the break this
    "while loop" would run indefinitely as there is nothing
    in the loop's body, which changes the condition: once True -
    - forever True

    Additionally: alphabet[x] is invariably a one-character-string,
    so char in alphabet[x] should be equivalent to
    char == alphabet[x] ---  did you think of this or did you
    intend something completely different?)

I must confess, that i don't fully understand what your program is
intended to do. I would very much appreciate, if you could
assemble (for instance at first with paper and pencil) a sample-run
of the program with some chracteristic inputs and corresponding
outputs and post it here. Perhaps then I ( and other people on this
list ) could support you with more fruitful hints or
propositions to make it more clear an concise.

>Regards, Gregor

I see the problem. Thanks for pointing it out to me. Also, I'll change the 'again' input to more and do something like:

more = raw_input("Would you like to convert more text or numbers [y/n]? ")
if more in 'Yy':
    pass ##because this continues the loop. after all, as you 
         ##said, 'again' is already true or this would not be
         ##arrived at
elif more in 'Nn':
    again = False

As for the second flaw, you are correct. Originally, i was attempting to have the program process the entire word inputed, not merely the first character, not realizing that happens without a loop there. That section will be changed to:

if char in alphabet[x]:
    print alphabet[x]

I tried to use that structure (if char == 'alphabet[x]':) but i encountered some problems probably unrelated. However, the structure i am currently using suits my tastes better in any case. I believe you are correct regarding the interchangeability of the two. 

This program, called 'Multivert', is eventually intended to be able to convert text (abc) into binary (01,10,11,etc.) into hexadecimal (1,2,3,4,5,6,7,8,9,A,B,C,etc.) into numbers (1,2,3,etc.) with any and all combinations.  Essentially, it would be a 4-way dictionary. If a person wanted to convert text into binary, they would type in text and select binary (currently the default choice until i fix bugs) or numbers or hexadecimal for those conversions. Then, the program would print the converted text. For example, "banana" in text would yield "10 01 1110 01 1110 01" in binary, "2 1 E 1 E 1" in hexadecimal, or "2 1 14 1 14 1" in numbers. Obviously, this is not all possible with the current code, which is why I'm still workin on it.  Similarly, any of the other entries ("10 01 1110 01 1110 01", "2 1 E 1 E 1", or "2 1 14 1 14 1") would yield "banana" in text and each other otherwise (each 'means' the same thing). I hope to be able to modify the program to distinguish between the four styles of input so that the user can begin with any style and convert it to any style. This would probably be accomplished in a very similar manner to what i have alread set up (try int(original), except Name/TypeError: convertxt(), else convertnum).  However, I will need to come up with a method for the program to distinguish hexadecimal from text, since both would yield an Error at int(original), and binary from numbers, since neither would yield an error. For the latter i will probably do something along the lines of:

...
    for char in original:
        try:
            int(original)
        except TypeError:
            convertxt()
        except ValueError:
            convertxt()
        else:
            if char in number[1:]:
                convertnum()
            else:
                convertbin() ##not yet in code
    again = raw_input("Would you like to convert more text or numbers [y/n]? ")
    if again in 'Yy':
        pass
    elif again in 'Nn':
        again = False
print "Thank you for using Multivert.  Multivert will now close"
time.sleep(1.1)
import sys
sys.exit()

I realize this would not work exactly as is, but it is a start.

"n thats the way the cookie crumbles"

America's Favorite Cookie


More information about the Tutor mailing list