[Tutor] Help - Using Sort and Join

Dave Angel d at davea.name
Mon Oct 22 22:15:03 CEST 2012

On 10/22/2012 03:57 PM, Daniel Gulko wrote:
> Hi Python Tutor,
> I have an issue trying to figure out how to print out final answers using sort and join functions.
> Assignment Specification:
> make a function that is a magic eight ball emulator. emulator will be a function that returns one of the possible answers. Make another function that runs the emulator over and over again until user wants to quit, printing answer each time. When user quits, present all answers that the user got again, but present them in alphabetical order. Use the join() function available for strings for this.
> Below is an example output from my code:
> ask a question (or press 'enter' to quit): will I be rich
> You may rely on it.
> ask a question (or press 'enter' to quit): will I be poor
> It is decidedly so.
> ask a question (or press 'enter' to quit): why is that
> Better not tell you now.
> As my code is written when user presses enter I simply break and print this current output:
> Have a nice day
> What I want is when user exits for my code to take all previous answers, joining them and sorting them like below:
> ask a question (or press 'enter' to quit):
> You may rely on it. It is decidedly so. Better not tell you now.
> My code works right now but I am missing the final part in how to join and sort all answers. Any suggestions or examples is helpful as I 
> am very new with Python.
> Current code is attached for viewing.
> Thanks,
> Dan

You should assume that people cannot get an attachment.  Some can and
some cannot.  Just put the code inline, and make sure you're writing a
text message.  (Your current one is html, another flaw that can cause
some to not get it the way you sent it)

Anyway, if you want to present a summary at the end, you'll have to
build a list as you go.  At the beginning of your function, create an
empty list.  Then append to it each message you're outputting.  Then
when you fall out of the loop, you can manipulate that list as your
assignment says, first sorting it, then using join to create a single
string from the list.

Also, you don't need an elif, since you already know the condition there
will be true.  Just use if and else, and just use a break under else. 
Then outside the loop, you can do as much as you like to give the final



More information about the Tutor mailing list