[Tutor] Print Help
Dave Angel
davea at ieee.org
Wed Dec 29 14:27:05 CET 2010
On 01/-10/-28163 02:59 PM, delegbede at dudupay.com wrote:
> #§¶Ú%¢Šh½êÚ–+-jwm…éé®)í¢ëZ¢w¬µ«^™éí¶qªë‰ë–[az+^šÈ§¶¥ŠË^ןrœ’)àë)¢{
°*'½êí²ËkŠx,¶–Š$–)
`·Ï@Ý"žÚ
Not sure why the quoting of this particular message didn't work. Since
I have no problem with anyone else's message, I suggest it's one of your
settings.
Recopying your original:
>I want to loop over a list and then print out a one statement that
>carries all the items in the list e.g
>
>def checking(responses):
>
> ''' Converts strings that look like A2C3D89AA90 into
> {'A':'2', 'C':'3', 'D':'89', 'AA':'90'}'''
>
> responses=dict(re.findall(r'([A-Z]{1,2})([0-9]+)',
>responses.upper())) if responses else {}
> for key in responses.keys():
> count = []
> if key in ['A'] and int(responses[key]) not in
>range(1,3): count += key
> elif key in ['B', 'G', 'T', 'U', 'V', 'W', 'X'] and
>int(responses[key]) not in range(1, 3): count += key
> elif key in ['H', 'J', 'K', 'M', 'N', 'P',
>'Q','R','S'] and int(responses[key]) not in range(1, 6): count += key
> elif key in ['D', 'E'] and int(responses[key]) == 4:
>print 'accepted'
> for err in count:
> count = ", ".join(count)
>
> print "Invalid Message: You specified out of range values
>for %s" % (", ".join(count))
>
>What I am expecting is:
>Invalid Message: You specified out of range values for a,s,d,r,t,u
>
>Assuming those were the values that are incorrect.
Hi Delegbede,
First comment is that your indentation is wrong, and the code won't
compile. Did you want your print to line up with "for key" or with "elif" ?
Second is that you keep defining count to have different types. Choose
what type it's supposed to have, and fix all the places that would make
it different. Your comments imply you want it to be a list, which is
what you initialize it to. But then you concatenate a string to it,
which will give an error. You probably wanted to append to it, using
counts.append(key)
Then you have a nonsensical twoline loop: for err in count, but inside
the loop you change count. Delete both those lines, as you're going to
use join inside your print statement instead.
Finally, you reassign the counts list inside the loop, so it will only
show the last values found. Move the counts=[] outside the outermost loop.
Fix the code up so it doesn't get any compile or runtime errors, and if
you can't, post a complete traceback for the error you can't fix.
And if you can run it without any errors, but the output is wrong, show
us what it actually prints, not just what you'd like it to.
DaveA
More information about the Tutor
mailing list