newbie problem with 'ord' function 
Author Message
 newbie problem with 'ord' function

Hello!

I'm a newbie to programming in Python. I am doing a project of my own
that involves using 'ord' function. My code includes this list:
   numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

Now, I would like to print out each one separately (this's not the
problem:)) and I would also like to print out the ASCII code for the
number / integer in question. Like this:
   NUMBER   ASCII Code
        1       49      
        2       50      etc.

The problem is I can't write the code right for the 'ord' function. For
example, I have tried this one code:
        for x in numbers:
                print x, ord([',x,']) (OR print x, ord(',x,')) etc.

What would do the trick? I am very grateful for any help.
Best wishes,
        Tuomas



Wed, 21 May 2003 03:00:00 GMT  
 newbie problem with 'ord' function

Quote:
>    numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>    NUMBER   ASCII Code
> 1 49
> 2 50 etc.

> What would do the trick?

for x in numbers:
  print x, ord(`x`)  #not 'x' but `x`!

--
franken



Wed, 21 May 2003 03:00:00 GMT  
 newbie problem with 'ord' function
Tuomas Pellonpera write:

Quote:
> I'm a newbie to programming in Python. I am doing a project of my own
> that involves using 'ord' function. My code includes this list:
>    numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

> Now, I would like to print out each one separately (this's not the
> problem:)) and I would also like to print out the ASCII code for the
> number / integer in question. Like this:
>    NUMBER   ASCII Code
> 1 49
> 2 50 etc.

str(x) converts an integer to a string (it also works
with most other data types).  try this:

for x in numbers:
    print x, ord(str(x))

</F>



Wed, 21 May 2003 03:00:00 GMT  
 newbie problem with 'ord' function

Quote:

> Hello!

> I'm a newbie to programming in Python. I am doing a project of my own
> that involves using 'ord' function. My code includes this list:
>    numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

> Now, I would like to print out each one separately (this's not the
> problem:)) and I would also like to print out the ASCII code for the
> number / integer in question. Like this:
>    NUMBER   ASCII Code
>         1       49
>         2       50      etc.

> The problem is I can't write the code right for the 'ord' function. For
> example, I have tried this one code:
>         for x in numbers:
>                 print x, ord([',x,']) (OR print x, ord(',x,')) etc.

Try this:

Quote:
>>> for x in numbers:

...     print x, ord(str(x))
...
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
0 48

Your array contains integers from 1 to 9 then 0 (which comes before 1,
not after 9).  What you are trying to do is print out the codes for the
ASCII characters corresponding to those integers.  You only get ASCII
characters in a string, not from integers directly (which are internal
to the computer, not directly representable to humans).  So use str() to
convert those integers to their printable representation (base 10,
ASCII) and then use ord() to find the codes corresponding...



Wed, 21 May 2003 03:00:00 GMT  
 newbie problem with 'ord' function

Quote:

> Now, I would like to print out each one separately (this's not the
> problem:)) and I would also like to print out the ASCII code for the
> number / integer in question. Like this:
>    NUMBER   ASCII Code
>         1       49
>         2       50      etc.

I have a utility for this at
http://members.tripod.com/~edcjones/utility01.html

Ed Jones



Wed, 21 May 2003 03:00:00 GMT  
 newbie problem with 'ord' function
Here is another way to do it:

digits = "0123456789"

print "NUMBER", "ASCII Code"

for digit in digits:
    print digit, ord(digit)

This works because a string in python can also act as a list of characters.
You could expand the 'digits' string to contain other characters than
digits.

/Jesper


Quote:
> >    numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
> >    NUMBER   ASCII Code
> > 1 49
> > 2 50 etc.

> > What would do the trick?

> for x in numbers:
>   print x, ord(`x`)  #not 'x' but `x`!

> --
> franken



Fri, 23 May 2003 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. TCL equivalent of PHP's ORD() function

2. Newbie:'Entry' echo problem Unix/Win95

3. Newbie:SpecTcl 'Entry' echo problem

4. Problem with 'is integer' function

5. function declaration problem, with 'implicit none'

6. stream('file','c','seek ='x) problem

7. Language question : function'Access / function'unchecked_Access

8. (documentation 'some-function 'function)

9. Newbie: Ada function like C++'s atoi?

10. Please help newbie with function from Wilensky's CommonLISPcraft

11. Newbie: can't devide by function()

12. Problem with ''Mailbox''

 

 
Powered by phpBB® Forum Software