[Tutor] Problem formatting raw_input

Peter Anderson peter.anderson at internode.on.net
Fri Oct 31 06:24:00 CET 2008


Dj Gilcrease wrote:
> The simple answer is to just use chr(int(inNum))
>
> though here is how I would do it
>
> def convert_string_to_int(strInt):
>     try:
>         return int(strInt)
>     except ValueError:
>         return 0
>
> def getNumbers(output):
>     inNum = raw_input("Please enter an ASCII number\n(33 - 126,
> [Enter] to quit): ")
>     ascii_num = convert_string_to_int(inNum)
>     if ascii_num >= 33 and ascii_num <=126:
>         output.append(chr(ascii_num))
>         getNumbers(output)
>
> if __name__ == '__main__':
>     print "This script converts a sequence of ASCII numbers"
>     print "into the string of text that it represents."
>     print
>     output = []
>     getNumbers(output)
>     print output
>
> Dj Gilcrease
> OpenRPG Developer
> ~~http://www.openrpg.com
Dj,

Thanks for the suggestions; both work perfectly.  Can I ask a 
supplementary question please?

In the def convert_string... function why do you include the "except 
ValueError: / return 0" clause?

Regards,
Peter
-- 
*Peter Anderson*
There is nothing more difficult to take in hand, more perilous to 
conduct, or more uncertain in its success, than to take the lead in the 
introduction of a new order of things—Niccolo Machiavelli, /The Prince/, 
ch. 6


More information about the Tutor mailing list