| ï
I have writen a code to do the process you need.
And It is not only suitable for integers, but also decimals and
negatives! The main method is type conversion functions of Python as
float(), str() and so on. I wish it will give you some
illumination.
Juan Shen
#!/usr/local/bin/python
#Filename: float2list.py import sys
try:
a=float(raw_input('Input a number: ')) except ValueError: print 'Input is not a number.' sys.exit() print 'The input number is:',a #knock down number into list lista=list(str(a)) print 'A number as a list:',lista #Pack this number from list again
#Process negative number if lista[0] is '-': sign=-1 lista=lista[1:] size1=lista.index('.') size2=len(lista)-lista.index('.')-1 integer=sum([int(lista[i])*10**(size1-i-1) for i in range(0,size1)]) decimal=sum([float(lista[lista.index('.')+i])*10**(-i) for i in range(1,size2+1)]) a2=sign*(integer+decimal) print 'The number is packed again:',a2
|
_______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
