[Tutor] [newb] ascii questions

justinstraube at charter.net justinstraube at charter.net
Sun Dec 14 06:08:48 EST 2003


On Sat, 13 Dec 2003 18:36:58 -0700, bgailer at alum.rp... wrote:

>>Im working on this and am running into a couple problems I can't 
figure
>>out. Can anyone offer any hints or anything?
>Unfortunately I don' t know the objective of the program or the 
problems
>you are having, so I can't help. However there is opportunity to 
comment a
>bit about the program.

Thank you, Bob for still taking the time to reply. I accidentally hit 
send as I went to save the message.

My objective is to adjust the users name by a random # of steps either 
up or down. Within a range of the user given number and its opposite. 
Then return 5 variations and the option for more. This is similar to an 
"extrapolate word" feature I had seen in a random word generator 
program.

I was able to do this with dictionaries, name.lower(), and no [space] 
characters allowed. But am trying to use ord() and chr() to allow for 
upper and lowercase and the [space] characters. Im trying to filter out 
non-alpha ascii characters and when 'z' or 'a' are reached it would 
countinue its steps from the other end.

>>import random
>>def process(val):
>The function would be easier to read and understand if n and amnt were 
also
>passed as parameters

I had thought about this but thought in a smaller program I could get 
away with this. Though you do make a good point about readability for 
others.

>>     r = val + random.randint(n, amnt)
>>     if r < 0:
>>         r = r + 26
>>     elif r > 26:
>
>This will never happen!

Im not sure I understand why not. What Im thinking happens here is that 
say name[0] = 's' which is 19th in the alphabet, and say the random 
integer was 9 I would count to 26 and start back at zero and continue 
until 2 for 'b'.

>>         r = r - 26
>>     return r
>>count = 0
>>name = Uname = raw_input('What is your name? ')
>>while 1:
>>     #user # as an int and verifies its in range
>>     amnt = int(raw_input('Number 1 to 26? '))
>
>Consider exception handlers here for user canceling or not entering a 
number:
>    try:
>        amnt =raw_input('Number 1 to 26? ')
>    except KeyboardInterrupt:
>        sys.exit(0)
>    try:
>        amnt = int(amnt)
>    except ValueError:
>        print 'That is not a number'
>        continue

Ive seen exception handlers in my book but havent read up to it yet. Im 
still tripping over some of the earlier things.

>>     if amnt > 26:
>
>Do you mean:
>      if amnt > 26 or amnt < 1:

Yes, thanks.

>>         print 'That number is out of range'
>>         continue
>
>continue here is superfluous.

I read again about countinues and breaks and I think Ive got it now.

>>     else:
>>        #negetive user #
>
>do you mean NEGATE?

yes, thanks

>>         n = -amnt
>>         break
>>#processes the users name 5 times
>>while count < 6:
>>     #assigns the values of names
>>     name = Uname
>>     xname = ''
>>     #gets 1st characters value
>>     for x in name:
>
>x is not used; in fact it is overridden at x = process....

ok I see now. Ive to try my ideas to be sure though.

>>         name_x = ord(name[0])
>
>You could take advantage of the fact that x takes on successive 
characters
>in name:
>           name_x = ord(x)
>Then you would not need name = name[0:]
>
>>         #uppercase
>>         if name_x >= 65 and name_x <= 90:
>>             name_x = name_x - 65
>>             x = process(name_x) + 65
>>         #lowercase
>>         elif name_x >= 97 and name_x <= 122:
>>             name_x = name_x - 96
>>             x = process(name_x) + 96
>>         #if name[0] is a [space]
>>         elif name_x == 32:
>>             x = 32
>>         name = name[0:]
>>         #adds the values character to the processed name
>>         xname = xname + chr(x)
>>     count = count + 1
>>     print xname
>>     if count == 6:
>>         print
>>         print 'Hit Enter for more'
>>         print 'Or ctrl-c to quit'
>>         raw_input()
>>         count = 0


>Here is an alternate version, in which I've packaged the input 
processing
>in a new function getInput() that handles all the exceptions, put all 
the
>character handling in process(), avoided the upper/lower issue by 
getting a
>random number in (n, amnt), testing for negative, then adding the ord, 
and
>used list comprehension to construct xname, and used some other Python
>shortcuts. If we knew the purpose of the program there might be some 
more
>suggestions. See what you think.

Im reading over your other pointers and comments and also the alternate 
version. I havent tested this yet but from what Ive read already, I see 
a few things which help. Ill work on this some more and hopefully be a 
bit more gracefull when composing further messages. :)

Thanks,

Justin

>import random, string
>def getInput(prompt, low, high):
>     try:
>         r = raw_input(prompt)
>     except KeyboardInterrupt:
>         return None
>     if valueRange:
>         try:
>             r = int(r)
>         except ValueError:
>             print 'That is not a number'
>             r = None
>         if not low <= r <= high:
>             print 'That number is out of range'
>             r =  None
>     return r
>def process(nameChar, numericLimit):
>     r = random.randint(-numericLimit, numericLimit)
>     if r < 0:
>         r += 26
>     return chr(r + ord(nameChar))
>name = getInput('What is your name? ')
>while 1:
>     amnt = getInput('Number 1 to 26? ', 1, 26)
>     if amnt:
>         break
>for y in xrange(1,999999):
>     xname = ''.join([process(x, amnt) for x in name])
>     print xname
>     if not y % 6:
>         if not getInput('\nHit Enter for more\nOr ctrl-c to quit'):
>             break
>
>Bob Gailer
>bgailer at alum.rpi.




More information about the Tutor mailing list