[Tutor] [newb] ascii questions
Bob Gailer
bgailer at alum.rpi.edu
Sat Dec 13 20:36:58 EST 2003
At 11:48 AM 12/13/2003, justinstraube at charter.net wrote:
>Hello Tutors,
>
>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.
>My questions follow this little bit
>
>import random
>def process(val):
The function would be easier to read and understand if n and amnt were also
passed as parameters
> r = val + random.randint(n, amnt)
> if r < 0:
> r = r + 26
> elif r > 26:
This will never happen!
> 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
> if amnt > 26:
Do you mean:
if amnt > 26 or amnt < 1:
> print 'That number is out of range'
> continue
continue here is superfluous.
> else:
> #negetive user #
do you mean NEGATE?
> 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....
> 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
>
>
>Im not sure where the problem is in here. When I enter a name with all
>lower case and a lower number for amnt I get back the results I am
>expecting.
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.
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.edu
303 442 2625 home
720 938 2625 cell
-------------- next part --------------
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.548 / Virus Database: 341 - Release Date: 12/5/2003
More information about the Tutor
mailing list