Path: csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!takemy.news.telefonica.de!telefonica.de!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'operator': 0.03; 'elif': 0.04; 'elegant': 0.07; 'ugly': 0.07; 'cc:addr :python-list': 0.09; 'app,': 0.09; 'statements': 0.09; 'useful,': 0.13; 'ignore': 0.14; 'suggest': 0.15; '"are': 0.16; '"is': 0.16; '"no":': 0.16; '"yes":': 0.16; 'algorithm.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'instance:': 0.16; 'reboot': 0.16; 'tuple):': 0.16; 'utterly': 0.16; 'wrote:': 0.16; 'nested': 0.18; 'pointed': 0.18; 'thoughts': 0.18; 'tree': 0.18; 'gui': 0.18; 'windows': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'algorithm': 0.20; 'disable': 0.22; 'disabled': 0.22; 'questions:': 0.22; 'sep': 0.22; 'sorry,': 0.22; 'am,': 0.23; 'bit': 0.23; 'tried': 0.24; 'import': 0.24; 'implemented': 0.24; 'header:In-Reply-To:1': 0.24; 'all.': 0.24; 'mon,': 0.24; 'least': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; '14,': 0.27; 'idea': 0.28; "skip:' 10": 0.28; 'initial': 0.28; '"please': 0.29; 'disabling': 0.29; 'pile': 0.29; 'short,': 0.29; 'solution,': 0.29; 'random': 0.29; 'asked': 0.29; "i'm": 0.30; 'work.': 0.30; 'code': 0.30; 'certainly': 0.30; 'e.g.': 0.30; 'task': 0.30; 'anyone': 0.32; 'another': 0.32; "can't": 0.32; 'implement': 0.32; 'reported': 0.32; 'though,': 0.32; 'changed': 0.33; 'hell': 0.33; 'similar': 0.33; 'changing': 0.34; 'structure': 0.34; 'running': 0.34; 'add': 0.34; 'that,': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'follows:': 0.35; 'something': 0.35; 'problem.': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'should': 0.36; 'there': 0.36; 'subject:: ': 0.37; 'mac': 0.37; 'one,': 0.37; 'no,': 0.38; 'turned': 0.38; 'data': 0.39; 'does': 0.39; "didn't": 0.39; 'some': 0.40; 'questions': 0.40; 'your': 0.60; 'reach': 0.61; 'real': 0.62; 'telephone': 0.62; 'yes': 0.62; 'more': 0.63; 'our': 0.64; 'experience.': 0.66; 'account': 0.66; 'talking': 0.67; 'life': 0.67; 'phone': 0.68; 'finally': 0.70; 'power': 0.72; 'transfer': 0.73; 'sounds': 0.76; 'hoping': 0.77; 'gas': 0.83; 'chrisa': 0.84; 'crowd': 0.84; 'data-driven': 0.84; 'etc,': 0.84; 'etc..': 0.84; 'problem.")': 0.84; 'to:none': 0.91; '"how': 0.91; 'inefficient': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type:content-transfer-encoding; bh=XVlbF+x8HHixl1h5NH9VCOclFJFVAo1lqu1y0SOjx68=; b=wvU6KL+pu6j4KWqISdA/DKgkf7TJuKHxtH2ag20OelLie5ZdktAMVS0HMvNYXBs6ly +I9SGw1/Fs3LBNc3ihzLKT+djRZWfnXOOXRslcicDTe2thvp/Haz82GwOZCW9Mbe/f+n 5sApW/fsyan97NjTSP5S82yn67DV55xwKVp6hiiLUb/wlLAsNzI8lbqowBfVEB3UYuWR mXPxXaIecCkXfSqFd/yNy4swrYSnjwOoDRUmHIqA38kZZ78XRN+CnnsgT621hhPV7vMv l1CEc9HPfxiC8cNS7DQNqOkoUGoDnkm9eZahs9ZeHJ9Hsppczv4V+Irpvkbc2uH3Iwuf W0HQ== MIME-Version: 1.0 X-Received: by 10.50.3.66 with SMTP id a2mr11630768iga.92.1442156395903; Sun, 13 Sep 2015 07:59:55 -0700 (PDT) In-Reply-To: <6ae5c632-b061-4b86-87ae-24c50435445b@googlegroups.com> References: <6ae5c632-b061-4b86-87ae-24c50435445b@googlegroups.com> Date: Mon, 14 Sep 2015 00:59:55 +1000 Subject: Re: Phone Tree From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 89 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1442156404 news.xs4all.nl 23777 [2001:888:2000:d::a6]:60652 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:96504 On Mon, Sep 14, 2015 at 12:39 AM, Azureaus wrote: > The first task is to implement a 'troubleshooting program' similar to wha= t a phone operator would have when you phone up with a problem. We all know= the type when you phone up your ISP, 'have you turned it off and on again?= ', "have you changed your telephone filter' etc.. > > It states there should be at least 10 questions and that the user should = reach a solution, e.g. 'replace your power cable'. There and my initial rea= ction was that this could be achieved by lots of if/else statements with ea= ch question running onto another one, ugly but it would certainly work. One= of them pointed out how inefficient this was and asked if there was anothe= r way, they hated writing out tons of if/elif/else statements. > > Does anyone have any ideas for a more elegant solution? My thoughts are t= hat I could use a tree data structure and hence make traversing the tree re= cursive based on yes or no answers. I'm happy to put the time in to explain= these more complex ideas, I'm just hoping those with more expertise than m= yself could either help verify the idea or suggest alternatives. > This sounds broadly similar to a Twenty Questions game. You may be able to find some neat implementations of that, which could be reworked into what you want. A more cynical algorithm could be implemented as follows: import random questions =3D [ # The IT Crowd "Hello, IT, have you tried turning it off and on again?", # The Bastard Operator From Hell "How long has this been happening?", "Has anyone else had this problem?", "Is it your floor that has the Gas Leak?", # Standard ISP questions "Have you tried disabling your antivirus?", "Are we talking to the authorized account holder?", # Add additional questions as required ] random.shuffle(questions) questions =3D questions[::2] # Ask about half of them for q in questions: response =3D input(q) # raw_input in Py2 # Utterly ignore the response print("I'm sorry, nobody else has reported this problem, so it cannot be a real problem.") I may or may not be basing this on real life experience. For something a bit more useful, though, what you might have is a nested pile of tuples. For instance: questions =3D ( # First the question "Have you rebooted your computer?", # Then the if-no response "Please reboot your computer, then call back.", # Finally the if-yes response ("Are you running Windows?", ("Are you using a Macintosh?", "I'm sorry, we only support Windows and Mac OS.", "Let me transfer you to our Macintosh support people.", ), ("Have you disabled your antivirus?", "Please disable your AV and try again.", ("Is it your floor that has the gas leak?", "I'm sorry, I can't help you.", "Strike a match. All your problems will disappear.", ), ), ), ) while isinstance(questions, tuple): q, no, yes =3D questions response =3D input(q) if response =3D=3D "yes": questions =3D yes elif response =3D=3D "no": questions =3D no else: print("I'm sorry, I didn't understand that response.") print(questions) # is now the solution This is a data-driven algorithm. The code is extremely short, and is fundamentally about the UI; you could change it completely (a GUI app, text-to-speech and DTMF for IVR, etc, etc) without changing the question structure at all. Is this the kind of tree structure you had in mind? ChrisA