<br><br><div class="gmail_quote">On 3 October 2012 04:39, Palice Fan <span dir="ltr"><<a href="mailto:magicwizardstar@gmail.com" target="_blank">magicwizardstar@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>Hello </div><div>i got stuck with the last bit of my programming practice. </div><div>Can somebody help me?</div><div>Write a program to read through a mail log, and figure out who had the most<br>messages in the file. The program looks for “From” lines and takes the second parameter on<br>

those lines as the person who sent the mail.<br>The program creates a Python dictionary that maps the sender’s address to the total number of<br>messages for that person.<br>After all the data has been read the program looks through the dictionary using a maximum loop<br>

(see Section 5.7.2) to find who has the most messages and how many messages the person has.</div><div><br>Enter a file name: mbox-short.txt<br><a href="mailto:cwen@iupui.edu" target="_blank">cwen@iupui.edu</a> :5<br>Enter a file name: mbox.txt<br>

<a href="mailto:zqian@umich.edu" target="_blank">zqian@umich.edu</a> :195</div><div> </div><div>Instead of printing off a number beside the email, i got another email and i dont know how to fix it.</div></blockquote><div>
<br></div><div>For future reference, please either include the source code in the email as text or as text attachment.  A screen capture means I have to retype all your code (and in this case some data) in order to have a look at your problem.  Not fun.  </div>
<div><br></div><div>To fix your problem you have to reverse engineer what's going on in your program.  I'll try and walk you through a little thought process in figuring out what's going on to try and help you.  </div>
<div><br></div><div>The last statement in your program (which is where the error is apparent) prints a fixed email address followed by a value that's assigned earlier on in a loop from the "values" variable. Consequently you should carefully inspect your code and ask yourself how it's possible that an email address instead of a number is being assigned to the "values" variable and thereby eventually to the "max" variable.  (By the way, note that "max" is not recommended as a variable name since max is also a built-in function in Python and so by declaring a variable with the same name you're hiding (known as shadowing) the Python function.  You can see there's something special about it by the fact that IDLE colours it purple, which should tip you off.)  But anyway, back to your "max" variable and "values" variable, we now look back carefully at the loop to see how or where we might be picking up email addresses when we should be getting integer counts... Let's look carefully at the loop declaration:</div>
<div><br></div><div>for values in messages:</div><div><br></div><div>Hmmm, this is looping directly over the dictionary "messages".  What is returned when you iterate directly over a dict like that?  (Hint, it's not the values, but the keys... e.g. the email addresses.)  Add some print statements in your loop so you can see what happens when it runs, for example: </div>
<div><br></div><div><div>print 'Starting iterating over "messages" dict'</div><div>for values in messages:</div><div>    print 'Value of "values" this iteration =', values</div><div>    if max is None or values > max:</div>
<div>        print 'Updating max...'</div><div>        max = values</div><div>    print 'Value of "max" after this iteration =', max</div></div><div><br></div><div>If you apply similar changes to your program and run that you'll see why the program doesn't work -- "values" is being assigned the keys (email addresses) from the dict, not the values.  It should also become clear that basically "values" is also a bad choice for the items being iterated over in the messages dict and is perhaps adding to the confusion, better would be:</div>
<div><br></div><div>for email_sender in messages:</div><div><br></div><div>This would make it clear that the items being iterated over are in fact the email addresses.  It's always a good idea to use descriptive specific names in your programs, not least because you yourself also need to read and understand your own code.  Anyway, then later in your loop it's then obvious that you can't just do:</div>
<div><br></div><div>    if max is None or values > max:</div><div>        max = values</div><div><br></div><div>(or if we use my suggested renaming)</div><div><br></div><div>    if max is None or email_sender > max:</div>
<div>        max = email_sender</div><div><br></div><div>Instead you want to retrieve the actual value (count) from the dict for that specific email sender, e.g.</div><div><br></div><div><div>    if max is None or messages[email_sender] > max:</div>
<div>        max = messages[email_sender]</div></div><div><br></div><div>... and with that I've now basically explained the essence of your first main problem.</div><div><br></div><div>However there remains another major flaw. Why are we assigning and outputting '<a href="mailto:cwen@iupui.edu">cwen@iupui.edu</a>' as the email address with the maximum number of emails, for any input?  Clearly that can't be right - if the input changes and another email address has the highest count then this code will output the wrong result.  So in addition to saving the max count, you must also save the max sender in the loop.  I think that's enough for now, see if you can fix your program given the above hints and if not post back again.</div>
<div><br></div><div>HTH,</div><div><br></div><div>Walter</div><div><br></div></div>