[Tutor] Problem formatting raw_input
bgailer at gmail.com
Fri Oct 31 19:50:19 CET 2008
Peter Anderson wrote:
> 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):
>> 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:
I would avoid recursion. Save that for recursive algorithms. An ordinary
loop is easier to read/maintain and you will not run out of recursion
depth. Also give user a meaningful error message.
inNum = raw_input("Please enter an ASCII number\n(33 - 126,
[Enter] to quit): ")
if not inNum:
ascii_num = convert_string_to_int(inNum)
if ascii_num >= 33 and ascii_num <=126:
print "Input must be an integer in range 33..126"
>> if __name__ == '__main__':
>> print "This script converts a sequence of ASCII numbers"
>> print "into the string of text that it represents."
>> output = 
>> print output
>> Dj Gilcrease
>> OpenRPG Developer
> 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?
try: must be followed by except or finally.
Chapel Hill NC
When we take the time to be aware of our feelings and
needs we have more satisfying interatctions with others.
Nonviolent Communication provides tools for this awareness.
As a coach and trainer I can assist you in learning this process.
What is YOUR biggest relationship challenge?
More information about the Tutor