[Tutor] (no subject)

Dragonfirebane at aol.com Dragonfirebane at aol.com
Mon Jun 7 23:36:20 EDT 2004


'original comes from farther down in the code:

again = True
while again:
  original = raw_input("Please enter numbers or text to be converted. ")
  try:
      int(original)
      pass
  except ValueError:
      convertxt()
      pass
  else:
      if char in number:
          convertnum()
          pass
  again = raw_input("Would you like to convert more text or letters [y/n]? ")
  if again in 'Yy':
      again = True
  elif again in 'Nn':
      again = False
print "Thank you for using Multivert.  Multivert will now close"
time.sleep(1.1)
import sys
sys.exit()

also, since the program is a work in progress, the if-elif-else statement hasn't been completed and won't be pending functionality of the if-elif part.  You're probably right about the "for-in" being what i thought would work, depsite not being what i intended.

Thanks for the help,
Orri
In a message dated 6/7/2004 8:46:01 PM Eastern Daylight Time, adeleinandjeremy at yahoo.com writes:

> > Hello all,
> > 
> > As far as I know, char is a valid variable describing characters in
> > a user response, correct?
> 
> It is a valid variable, period. But, variables need to be assigned
> values before being referenced....
> 
> > 
> > Traceback (most recent call last):
> >   File "C:\Documents and Settings\Orri
> > Ganel\Desktop\Hexadecimal-Binary-Text.py", line 32, in ?
> >     convertxt()
> >   File "C:\Documents and Settings\Orri
> > Ganel\Desktop\Hexadecimal-Binary-Text.py", line 2, in convertxt
> >     if char in alphabet[:26]:
> > NameError: global name 'char' is not defined
> > 
> > come up when i run the following program?:
> > 
> > def convertxt():
> >     if char in alphabet[:26]:
> >         print binary[alphabet.index(original)]
> >     elif char in alphabet[26:]:
> >         print binary[alphabet.index(original)]
> 
> Maybe I am missing something here, but if this is your actual code,
> how would the interpreter know if 'char' is in the list, when you
> haven't told it what 'char' is? Maybe you are confusing the if-in
> construct with the for-in? ``for char in alphabet[:26]:'' is valid,
> but not what you want. Perhaps you meant to pass 'char' as an
> argument? And where is 'original' coming from?
> 
> Also, you have an if-elif, but no else. Maybe if-else would be more
> appropriate? And the actions to be taken are the same in both cases,
> so why use a selection structure at all?
> 
> If I am reading the intent of your code correctly, I would prefer the
> following:
> 
> def convertxt(char):
>    return binary[alphabet.index(char)]
> 
> Note that I have changed the print statement to a return statement so
> as to avoid side-effects. Print the returned value elsewhere, if
> that's what's required - in this case it would be more normal to do
> something like ``new_text += convertxt(next_char)'' - of 
> course,
> having first properly assigned the variables 'new_text' and
> 'next_char'.
> 
> HTH
> - Jeremy

"n thats the way the cookie crumbles"

America's Favorite Cookie



More information about the Tutor mailing list