Re: [Tutor] creat a program that reads frequency of words in file
On 02/06/15 00:42, Alan Gauld wrote: forwordintext: ifwordnot inwords: words[word] =1 else: words[word] +=1 Look into the setdefault() method of dictionaries. It can replace the if/else above. On reflection, the get() method might be even more useful. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Fwd: Re: Parsing/Crawling test College Class Site.
Forwarding to list. Always use ReplyAll (or reply List if you have that option) to include the list. Forwarded Message Subject:Re: [Tutor] Parsing/Crawling test College Class Site. Date: Mon, 1 Jun 2015 20:42:48 -0400 From: bruce To: Alan Gauld Seriously embarrassed!! The issue that's happening is the process doesn't generate the page with the classlist!! forgot to mention why I was posting this... On Mon, Jun 1, 2015 at 8:40 PM, bruce wrote: Hi Alan. Thanks. So, here goes! The target site is: https://my.unlv.nevada.edu/psc/lvporprd/EMPLOYEE/HRMS/c/COMMUNITY_ACCESS.CLASS_SEARCH.GBL The following is a sample of the test code, as well as the url/posts of the pages as produced by the Firefox/Firebug process. Basically, a user accesses the initial url, and then selects a couple of items on the page, followed by the "Search" btn at the bottom of the page. The items (in order to be input/selected) by the user are: -subject (insert ACC) for accounting -uncheck "Show Open Classes Only" -select the "Additional Search Criteria" expansion (bottom of the page) --In the "Days of Week" dropdown, select the "include any of these days" --select all days except Sat/Sun finally, select the "Search" btn, which generates the actual class list for the ACC dept. During each action, the app might generate ajax which updates/interfaces with the backend. All of this can be seen/tracked (I think) if you have the Firebug plugin for firefox running, where you can then track the cookies/post actions. The same data can be generated running LiveHttpHeaders (or some other network app). The process is running on centos, using V2.6.6. The test app is a mix of standard py, and liberal use of the system curl cmd. In order to generate one of the post vars, XPath is used to extract the value from the initial generated file/content. #!/usr/bin/python #- # #FileName: #unlvClassTest.py # #Creation date: #jun/1/15 # #Modification/update: # # #Purpose: #test generating of the psoft dept data # #Usage: #cmdline unlvClassTest.py # #App Logic: # # # # # # #- #test python script import subprocess import re import libxml2dom import urllib import urllib2 import sys, string import time import os import os.path from hashlib import sha1 from libxml2dom import Node from libxml2dom import NodeList import hashlib import pycurl import StringIO import uuid import simplejson from string import ascii_uppercase #=== execfile('/apps/parseapp2/ascii_strip.py') execfile('dir_defs_inc.py') appDir="/apps/parseapp2/" # data output filename datafile="unlvDept.dat" # global var for the parent/child list json plist={} cname="unlv.lwp" # if __name__ == "__main__": # main app # # get the input struct, parse it, determine the level # cmd="echo '' > "+datafile proc=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE) res=proc.communicate()[0].strip() cmd="echo '' > "+cname proc=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE) res=proc.communicate()[0].strip() cmd='curl -vvv ' cmd=cmd+'-A "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.11) Gecko/2009061118 Fedora/3.0.11-1.fc9 Firefox/3.0.11"' cmd=cmd+' --cookie-jar '+cname+' --cookie '+cname+'' cmd=cmd+'-L "http://www.lonestar.edu/class-search.htm";' #proc=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE) #res=proc.communicate()[0].strip() #print res cmd='curl -vvv ' cmd=cmd+'-A "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.11) Gecko/2009061118 Fedora/3.0.11-1.fc9 Firefox/3.0.11"' cmd=cmd+' --cookie-jar '+cname+' --cookie '+cname+'' cmd=cmd+'-L "https://campus.lonestar.edu/classsearch.htm";' #proc=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE) #res1=proc.communicate()[0].strip() #print res1 #initial page cmd='curl -vvv ' cmd=cmd+'-A "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.11) Gecko/2009061118 Fedora/3.0.11-1.fc9 Firefox/3.0.11"' cmd=cmd+' --cookie-jar '+cname+' --cookie '+cname+'' cmd=cmd+'-L "https://my.unlv.nevada.edu/psc/lvporprd/EMPLOYEE/HRMS/c/COMMUNITY_ACCESS.CLASS_SEARCH.GBL";' proc=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE) res2=proc.communicate()[0].strip() #print cmd+"\n\n" print res2 sys.exit() # s contains HTML not XML text d = libxml2dom.parseString(res2, html=1) #---Form selpath="//input[@id='ICSID']//attribute::value" sel_ = d.xpath(selpath) if (len(sel_) == 0): #--print svpath #--print "ll" #--print " select error" sys.exit() val="" ndx=0 for a in sel_: val=a.textContent.strip() print val #sys.exit() if(val==""): sys.exit() #build the 1st po
[Tutor] creating a dictionary for capital quiz program
Good evening, As you may have noticed i am really struggling with functions and dictionaries. I need to figure out why this program is allowing me to continue entering incorrect data instead of telling me my answer is incorrect. also at the end it’s not tallying the incorrect/correct responses properly. please any help would be appreciated. Write a program that creates a dictionary containing the U.S. States as keys and their capitals as values. (Use the internet to get a list of the states and their capitals.) The program should then randomly quiz the user by displaying the name of a state and asking the usr to enter that state's capital. The program should keep a count of the number of correct and incorrect responses. (As an alternative to the US states, the program can use the names of countries and their capitals.)""" import pickle def main(): right = 0 wrong = 0 capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', \ \ "Arizona": 'Phoenix', \ \ 'Arkansas': 'Little Rock', 'California': 'Sacramento', \ \ 'Colorado': 'Denver', \ \ 'Connecticut': 'Hartford', 'Delaware': 'Dover', \ \ 'Florida': 'Tallahassee', \ \ 'Georgia': 'Atlanta', 'Hawaii': 'Honolulu', \ \ 'Idaho': 'Boise', \ \ 'Illinois': 'Springfield', 'Indiana': 'Indianapolis', \ \ 'Iowa': 'Des Moines', \ \ 'Kansas': 'Topeka', 'Kentucky': 'Frankfort', \ \ 'Louisiana': 'Baton Rouge', \ \ 'Maine': 'Augusta', 'Maryland': 'Annapolis', \ \ 'Massachusetts': 'Boston', \ \ 'Michigan': 'Lansing', 'Minnesota': 'Saint Paul', \ \ 'Mississippi': 'Jackson', \ \ 'Missouri': 'Jefferson City', 'Montana': 'Helena', \ \ 'Nebraska': 'Lincoln', \ \ 'Nevada': 'Carson City', 'New Hampshire': 'Concord', \ \ 'New Jersey': 'Trenton', \ \ 'New Mexico': 'Santa Fe', 'New York': 'Albany', \ \ 'North Carolina': 'Raleigh', \ \ 'North Dakota': 'Bismarck', 'Ohio': 'Columbus', \ \ 'Oklahoma': 'Oklahoma City', \ \ 'Oregon': 'Salem', 'Pennsylvania': 'Harrisburg', \ \ 'Rhode Island': 'Providence', \ \ 'South Carolina': 'Columbia', \ \ 'South Dakota': 'Pierre', 'Tennessee': 'Nashville', \ \ 'Texas': 'Austin', 'Utah': 'Salt Lake City', \ \ 'Vermont': 'Montpelier', \ \ 'Virginia': 'Richmond', 'Washington': 'Olympia', \ \ 'West Virginia': 'Charleston', \ \ 'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'} for k in capitals.keys(): state = input('Enter the capital of '+k+' :') if state.upper() == capitals[k].upper(): right += 1 print('Correct') else: wrong += 1 print('Incorrect') choice = input('Do you want to play again y/n: ') if choice.upper() == 'N': print('end of game') else: choice.upper() != 'Y' print("invalid choice") print('Number of correct answers is: ', right) print("Number of incorrect answers is:", wrong) main() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creat a program that reads frequency of words in file
thanks on the help. I am now stuck on this program for quizzing on state capitals. Do you mind taking a look please? I can’t get it to tell me the answer is incorrect it just keeps asking me for capitals whether the answer is right or wrong. It also is not giving me correct counts for correct and incorrect answers. Any help would be appreciated. Not sure if i turned HTML. my laptop is fairly new and I’m still assimilating to iOS. Please let me know if the code is hard to read. Thanks ___ Write a program that creates a dictionary containing the U.S. States as keys and their capitals as values. (Use the internet to get a list of the states and their capitals.) The program should then randomly quiz the user by displaying the name of a state and asking the usr to enter that state's capital. The program should keep a count of the number of correct and incorrect responses. (As an alternative to the US states, the program can use the names of countries and their capitals.)""" import pickle def main(): right = 0 wrong = 0 capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', \ \ "Arizona": 'Phoenix', \ \ 'Arkansas': 'Little Rock', 'California': 'Sacramento', \ \ 'Colorado': 'Denver', \ \ 'Connecticut': 'Hartford', 'Delaware': 'Dover', \ \ 'Florida': 'Tallahassee', \ \ 'Georgia': 'Atlanta', 'Hawaii': 'Honolulu', \ \ 'Idaho': 'Boise', \ \ 'Illinois': 'Springfield', 'Indiana': 'Indianapolis', \ \ 'Iowa': 'Des Moines', \ \ 'Kansas': 'Topeka', 'Kentucky': 'Frankfort', \ \ 'Louisiana': 'Baton Rouge', \ \ 'Maine': 'Augusta', 'Maryland': 'Annapolis', \ \ 'Massachusetts': 'Boston', \ \ 'Michigan': 'Lansing', 'Minnesota': 'Saint Paul', \ \ 'Mississippi': 'Jackson', \ \ 'Missouri': 'Jefferson City', 'Montana': 'Helena', \ \ 'Nebraska': 'Lincoln', \ \ 'Nevada': 'Carson City', 'New Hampshire': 'Concord', \ \ 'New Jersey': 'Trenton', \ \ 'New Mexico': 'Santa Fe', 'New York': 'Albany', \ \ 'North Carolina': 'Raleigh', \ \ 'North Dakota': 'Bismarck', 'Ohio': 'Columbus', \ \ 'Oklahoma': 'Oklahoma City', \ \ 'Oregon': 'Salem', 'Pennsylvania': 'Harrisburg', \ \ 'Rhode Island': 'Providence', \ \ 'South Carolina': 'Columbia', \ \ 'South Dakota': 'Pierre', 'Tennessee': 'Nashville', \ \ 'Texas': 'Austin', 'Utah': 'Salt Lake City', \ \ 'Vermont': 'Montpelier', \ \ 'Virginia': 'Richmond', 'Washington': 'Olympia', \ \ 'West Virginia': 'Charleston', \ \ 'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'} for k in capitals.keys(): state = input('Enter the capital of '+k+' :') if state.upper() == capitals[k].upper(): right += 1 print('Correct') else: wrong += 1 print('Incorrect') choice = input('Do you want to play again y/n: ') if choice.upper() == 'N': print('end of game') else: choice.upper() != 'Y' print("invalid choice") print('Number of correct answers is: ', right) print("Number of incorrect answers is:", wrong) main() > On Jun 1, 2015, at 7:42 PM, Alan Gauld wrote: > > I've CCd the list. Please use reply all when responding to the list. > Also please use plain text as HTML/RTF doesn't work on all > systems and code in particular often gets mangled. > > On 01/06/15 23:59, Stephanie Quiles wrote: >> Hello again, >> >> here is the final code… I think :) please see below. Is this is the easiest >> way to go about it? I appreciate your assistance! >> >> defmain(): >> words = {} >> count =0 > > Do you need count? What is its purpose? >> withopen('words.txt')asdata: >> forlineindata: >> text = line.split() >> forwordintext: >> ifwordnot inwords: >> words[word] =1 >> else: >> words[word] +=1 > > Look into the setdefault() method of dictionaries. > It can replace the if/else above. > >> count +=1 >> print(words) > > Aside from the two comments above, good job! > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a dictionary for capital quiz program
Stephanie Quiles wrote: > Good evening, > > As you may have noticed i am really struggling with functions and > dictionaries. I need to figure out why this program is allowing me to > continue entering incorrect data instead of telling me my answer is > incorrect. also at the end it’s not tallying the incorrect/correct > responses properly. please any help would be appreciated. > for k in capitals.keys(): > state = input('Enter the capital of '+k+' :') > if state.upper() == capitals[k].upper(): > right += 1 > print('Correct') > else: > wrong += 1 > print('Incorrect') When and how often is the line if state.upper() == capitals[k].upper(): executed? Hint: look at the indentation in the quoted code. PS: > capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', \ > \ >"Arizona": 'Phoenix', \ > \ >'Arkansas': 'Little Rock', 'California': 'Sacramento', \ You don't need these backslashes as long as you're inside parens, brackets or braces. Python will happily accept dicts, lists etc. that spread over multiple lines. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trouble using bioread to convert files from .acq to .mat
In a message of Mon, 01 Jun 2015 15:50:26 -0400, Ila Kumar writes: >Hello, > >I am a new Python user attempting to use bioread ( >https://pypi.python.org/pypi/bioread/0.9.5) to convert files from >aqknowledge to matlab. I am using a 64-bit PC, and I have downloaded >Matlab, Python, numpy, scipy and bioread. Can someone walk me through the >installation process for this package? I can't seem to get it to work. > >Thank you so much for your help!! >___ >Tutor maillist - Tutor@python.org >To unsubscribe or change subscription options: >https://mail.python.org/mailman/listinfo/tutor You are getting something like: Command python setup.py egg_info failed with error code 1 in /tmp/pip-build-T3JieI/bioread Storing debug log for failure in /home/lac/.pip/pip.log when you run pip, correct? So you go read the package documentation and discover that bioread uses easy_install (which you are beginning to learn to hate) instead of pip. So then you try that and you get crud like this: File "/usr/lib/python2.7/dist-packages/setuptools/sandbox.py", line 43, in _execfile exec(code, globals, locals) File "/tmp/easy_install-PGpger/bioread-0.9.5/setup.py", line 3, in ImportError: No module named ez_setup Correct? If this is your problem, go install this package: https://pypi.python.org/pypi/ez_setup And then install bioread, still with easy_install If this isn't your problem, write back with more of what is not working, please. Hope this is it :) Laura ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creat a program that reads frequency of words in file
On 02/06/15 02:35, Stephanie Quiles wrote: import pickle You don;t need pickle def main(): right = 0 wrong = 0 capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', \ \ "Arizona": 'Phoenix', \ You don't need the \ Inside {} you can put newlines as much as you like: 'Arkansas': 'Little Rock', 'California': 'Sacramento', 'Colorado': 'Denver', 'Connecticut': 'Hartford', 'Delaware': 'Dover', 'Florida': 'Tallahassee', etc... for k in capitals.keys(): state = input('Enter the capital of '+k+' :') This loop just keeps reading the inputs but does nothing with them. I suspect you intended to have the following lines inside the loop? if state.upper() == capitals[k].upper(): right += 1 print('Correct') else: wrong += 1 print('Incorrect') choice = input('Do you want to play again y/n: ') if choice.upper() == 'N': print('end of game') You need to provide a mechanism for exiting the loop. The break statement can do that for you. else: choice.upper() != 'Y' print("invalid choice") print('Number of correct answers is: ', right) print("Number of incorrect answers is:", wrong) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: Re: Parsing/Crawling test College Class Site.
On 02/06/15 08:27, Alan Gauld wrote: The following is a sample of the test code, as well as the url/posts of the pages as produced by the Firefox/Firebug process. I'm not really answering your question but addressing some issues in your code... execfile('/apps/parseapp2/ascii_strip.py') execfile('dir_defs_inc.py') I'm not sure what these do but usually its better to import the files as modules then execute their functions directly. appDir="/apps/parseapp2/" # data output filename datafile="unlvDept.dat" # global var for the parent/child list json plist={} cname="unlv.lwp" # if __name__ == "__main__": # main app It makes testing (and reuse) easier if you put the main code in a function called main() and then just call that here. Also your code could be broken up into smaller functions which again will make testing and debugging easier. # # get the input struct, parse it, determine the level # cmd="echo '' > "+datafile proc=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE) res=proc.communicate()[0].strip() Its easier and more efficient/reliable to create the file directly from Python. Calling the subprocess modyule each time starts up extra processes. Also you store the result but never use it... cmd="echo '' > "+cname proc=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE) res=proc.communicate()[0].strip() See above cmd='curl -vvv ' cmd=cmd+'-A "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.11) Gecko/2009061118 Fedora/3.0.11-1.fc9 Firefox/3.0.11"' cmd=cmd+' --cookie-jar '+cname+' --cookie '+cname+'' cmd=cmd+'-L "http://www.lonestar.edu/class-search.htm";' You build up strings like this many times but its very inefficient. There are several better options: 1) create a list of substrings then use join() to convert the list to a string. 2) use a triple quoted string to create the string once only. And since you are mostly passing them to Popen look at the docs to see how to pass a list of args instead of one large string, its more secure and generally better practice. cmd='curl -vvv ' cmd=cmd+'-A "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.11) Gecko/2009061118 Fedora/3.0.11-1.fc9 Firefox/3.0.11"' cmd=cmd+' --cookie-jar '+cname+' --cookie '+cname+'' cmd=cmd+'-L "https://campus.lonestar.edu/classsearch.htm";' #initial page cmd='curl -vvv ' cmd=cmd+'-A "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.11) Gecko/2009061118 Fedora/3.0.11-1.fc9 Firefox/3.0.11"' cmd=cmd+' --cookie-jar '+cname+' --cookie '+cname+'' cmd=cmd+'-L "https://my.unlv.nevada.edu/psc/lvporprd/EMPLOYEE/HRMS/c/COMMUNITY_ACCESS.CLASS_SEARCH.GBL";' proc=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE) res2=proc.communicate()[0].strip() print res2 sys.exit() Since this is non conditional you always exit here so nothing else ever gets executed. This may be the cause of your problem? # s contains HTML not XML text d = libxml2dom.parseString(res2, html=1) #---Form selpath="//input[@id='ICSID']//attribute::value" sel_ = d.xpath(selpath) if (len(sel_) == 0): sys.exit() val="" ndx=0 for a in sel_: val=a.textContent.strip() print val #sys.exit() if(val==""): sys.exit() #build the 1st post ddd=1 post="" This does nothing since you immediately replace it with the next line. post="ICAJAX=1" post=post+"&ICAPPCLSDATA=" post=post+"&ICAction=DERIVED_CLSRCH_SSR_EXPAND_COLLAPS%24149%24%241" post=post+"&ICActionPrompt=false" post=post+"&ICAddCount=" post=post+"&ICAutoSave=0" post=post+"&ICBcDomData=undefined" post=post+"&ICChanged=-1" post=post+"&ICElementNum=0" post=post+"&ICFind=" post=post+"&ICFocus=" post=post+"&ICNAVTYPEDROPDOWN=0" post=post+"&ICResubmit=0" post=post+"&ICSID="+urllib.quote(val) post=post+"&ICSaveWarningFilter=0" post=post+"&ICStateNum="+str(ddd) post=post+"&ICType=Panel" post=post+"&ICXPos=0" post=post+"&ICYPos=114" post=post+"&ResponsetoDiffFrame=-1" post=post+"&SSR_CLSRCH_WRK_SSR_OPEN_ONLY$chk$3=N" post=post+"&SSR_CLSRCH_WRK_SUBJECT$0=ACC" post=post+"&TargetFrameName=None" Since these are all hard coded strings you might as well have just hard coded the final string and saved a lot of processing. (and code space) cmd='curl -vvv ' cmd=cmd+'-A "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.11) Gecko/2009061118 Fedora/3.0.11-1.fc9 Firefox/3.0.11"' cmd=cmd+' --cookie-jar '+cname+' --cookie '+cname+'' cmd=cmd+'-e "https://my.unlv.nevada.edu/psc/lvporprd/EMPLOYEE/HRMS/c/COMMUNITY_ACCESS.CLASS_SEARCH.GBL?&"; This looks awfully similar to the code up above. Could you have reused the command? Maybe with some parameters - check out string formatting operations. eg: 'This string takes %s as a parameter" % 'a string' I'll stop here, its all getting a bit repetitive. Which is, in itself a sign that
Re: [Tutor] unittest with random population data
On 02/06/2015 07:59, Steven D'Aprano wrote: Please keep your replies on the tutor list, so that others may offer advice, and learn from your questions. Thanks, Steve On Mon, Jun 01, 2015 at 06:03:08PM +0100, Sydney Shall wrote: On 31/05/2015 00:41, Steven D'Aprano wrote: On Sat, May 30, 2015 at 12:16:01PM +0100, Sydney Shall wrote: I have written a unittest class which works OK. But the problem I have is that because I use the random module to populate my initial arrays, my data is not strictly predictable even though I am using seed(0). Please show us how you populate your arrays, because what you describe sounds wrong. Seeding to the same value should give the same sequence of values: py> import random py> random.seed(0) py> a = [random.random() for i in range(10**6)] py> random.seed(0) py> b = [random.random() for i in range(10**6)] py> a == b True Thank you for the advice Steven. I was of course aware that I had to use random.seed(0), which I had done. I was puzzled by the fact that it did not give me reprocibly results, which it did when I was learning python. But because you drew attention to the problem, I have looked at again. I surmised that perhaps I had put the seed statement in the wrong place. I have tried several places, but I always get the sam spread of results. Perhaps, to help get advice I should explain that I populate a a list thus: self.ucc = np.random.normal(self.mean_ucc, self.sigma_ucc, 200) This does give me list of 200 slightly different numbers. The mean_ucc is always 15.0 and the sigma value is always 3.75. The actual mean and sigma of the random numbers is checked that it is within 5.0% of 15.0 and 3.75 respectively. Following your advice I did a little test. I repeated a little test program that I have written, which gives me sensible and proper results. I repeated the test 8 times and a then noted a useful output result. When I plot the actual mean of the population used against the output result I chose, I obtain a perfect straight line, which I should. Now I still think that I am using the random.seed(0) either incorrectly or in the wrong place. If there is any other information that might clarify my problem, I will be grateful to be told. I would be most grateful for any guidance you may have, indicating where I should look for what I suspect is a beginner's error. -- Sydney Apolgies. I thought that I had dione so. I will be more careeful infuture. -- Sydney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a dictionary for capital quiz program
On Tue, Jun 2, 2015 at 3:43 AM, Peter Otten <__pete...@web.de> wrote: > Stephanie Quiles wrote: > >> Good evening, >> >> As you may have noticed i am really struggling with functions and >> dictionaries. I need to figure out why this program is allowing me to >> continue entering incorrect data instead of telling me my answer is >> incorrect. also at the end it’s not tallying the incorrect/correct >> responses properly. please any help would be appreciated. > >> for k in capitals.keys(): >> state = input('Enter the capital of '+k+' :') The above statement won't print the name of the capital. try something like this: state = input('Enter the capital of' + k + ':') >> if state.upper() == capitals[k].upper(): >> right += 1 >> print('Correct') >> else: >> wrong += 1 >> print('Incorrect') > > When and how often is the line > > if state.upper() == capitals[k].upper(): > > executed? > > Hint: look at the indentation in the quoted code. > > PS: > >> capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', \ >> \ >>"Arizona": 'Phoenix', \ >> \ >>'Arkansas': 'Little Rock', 'California': 'Sacramento', \ > > You don't need these backslashes as long as you're inside parens, brackets > or braces. Python will happily accept dicts, lists etc. that spread over > multiple lines. > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a dictionary for capital quiz program
I'm a newbie, but was able to tune it to correctly reply to user inputs. 1. My question is can it be optimized in any way? 2. Why (on Windows) do I have to give inputs in quotes not to cause an error (for ll input the error is ' NameError: name 'll' is not defined')? def main(): right = 0 wrong = 0 capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', "Arizona": 'Phoenix', \ 'Arkansas': 'Little Rock', 'California': 'Sacramento', \ 'Colorado': 'Denver', 'Connecticut': 'Hartford', 'Delaware': 'Dover', \ 'Florida': 'Tallahassee', \ 'Georgia': 'Atlanta', 'Hawaii': 'Honolulu', \ 'Idaho': 'Boise', \ 'Illinois': 'Springfield', 'Indiana': 'Indianapolis', \ 'Iowa': 'Des Moines', \ 'Kansas': 'Topeka', 'Kentucky': 'Frankfort', \ 'Louisiana': 'Baton Rouge', \ 'Maine': 'Augusta', 'Maryland': 'Annapolis', \ 'Massachusetts': 'Boston', \ 'Michigan': 'Lansing', 'Minnesota': 'Saint Paul', \ 'Mississippi': 'Jackson', \ 'Missouri': 'Jefferson City', 'Montana': 'Helena', \ 'Nebraska': 'Lincoln', \ 'Nevada': 'Carson City', 'New Hampshire': 'Concord', \ 'New Jersey': 'Trenton', \ 'New Mexico': 'Santa Fe', 'New York': 'Albany', \ 'North Carolina': 'Raleigh', \ 'North Dakota': 'Bismarck', 'Ohio': 'Columbus', \ 'Oklahoma': 'Oklahoma City', \ 'Oregon': 'Salem', 'Pennsylvania': 'Harrisburg', \ 'Rhode Island': 'Providence', \ 'South Carolina': 'Columbia', \ 'South Dakota': 'Pierre', 'Tennessee': 'Nashville', \ 'Texas': 'Austin', 'Utah': 'Salt Lake City', \ 'Vermont': 'Montpelier', \ 'Virginia': 'Richmond', 'Washington': 'Olympia', \ 'West Virginia': 'Charleston', \ 'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'} for k in capitals.keys(): state = input('Enter the capital of '+k+' :') if state.upper() == capitals[k].upper(): right += 1 print('Correct') else: wrong += 1 print('Incorrect') choice = input('Do you want to play again y/n: ') if choice.upper() == 'N': print('end of game') break elif choice.upper() != 'Y': print("invalid choice") print('Number of correct answers is: ', right) print("Number of incorrect answers is:", wrong) main() Regards, Jakub -Original Message- From: Tutor [mailto:tutor-bounces+jakub.zbudniewek=arimr.gov...@python.org] On Behalf Of Peter Otten Sent: Tuesday, June 02, 2015 9:43 AM To: tutor@python.org Subject: Re: [Tutor] creating a dictionary for capital quiz program Stephanie Quiles wrote: > Good evening, > > As you may have noticed i am really struggling with functions and > dictionaries. I need to figure out why this program is allowing me to > continue entering incorrect data instead of telling me my answer is > incorrect. also at the end it’s not tallying the incorrect/correct > responses properly. please any help would be appreciated. > for k in capitals.keys(): > state = input('Enter the capital of '+k+' :') > if state.upper() == capitals[k].upper(): > right += 1 > print('Correct') > else: > wrong += 1 > print('Incorrect') When and how often is the line if state.upper() == capitals[k].upper(): executed? Hint: look at the indentation in the quoted code. PS: > capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', \ > \ >"Arizona": 'Phoenix', \ > \ >'Arkansas': 'Little Rock', 'California': 'Sacramento', \ You don't need these backslashes as long as you're inside parens, brackets or braces. Python will happily accept dicts, lists etc. that spread over multiple lines. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor Wiadomość ta jest przeznaczona jedynie dla osoby lub podmiotu, który jest jej adresatem i może zawierać poufne i/lub uprzywilejowane informacje. Zakazane jest jakiekolwiek przeglądanie, przesyłanie, rozpowszechnianie lub inne wykorzystanie tych informacji lub podjęcie jakichkolwiek działań odnośnie tych informacji przez osoby lub podmioty inne niż zamierzony adresat. Jeżeli Państwo otrzymali przez pomyłkę tę informację prosimy o poinformowanie o tym nadawcy i usunięcie tej wiadomości z wszelkich komputerów. The information transmitted is intended only for the person or enti
Re: [Tutor] creating a dictionary for capital quiz program
ZBUDNIEWEK. JAKUB wrote: > I'm a newbie, but was able to tune it to correctly reply to user inputs. > 2. Why (on Windows) do I have to give inputs in quotes not to cause an > error (for ll input the error is ' NameError: name 'll' is not defined')? If you are running the script under Python 2 you should use raw_input() instead of input(). input() will take the user input and also run eval() on it: Python 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> input("? ") ? 1 + 1 2 >>> raw_input("? ") ? 1 + 1 '1 + 1' Python 3 has no raw_input() and input() will behave like raw_input() in Python 2. > 1. My question is can it be optimized in any way? In Python 2 capital.keys() builds a list. You can avoid that by iterating over the dict directly: for k in capitals: ... Not an optimization, but if the user enters neither Y nor N you might ask again instead of assuming Y. > def main(): > > right = 0 > wrong = 0 > capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', "Arizona": > 'Phoenix', \ >'Arkansas': 'Little Rock', 'California': 'Sacramento', \ >'Colorado': 'Denver', 'Connecticut': 'Hartford', >'Delaware': 'Dover', \ 'Florida': 'Tallahassee', \ >'Georgia': 'Atlanta', 'Hawaii': 'Honolulu', \ >'Idaho': 'Boise', \ >'Illinois': 'Springfield', 'Indiana': 'Indianapolis', \ >'Iowa': 'Des Moines', \ >'Kansas': 'Topeka', 'Kentucky': 'Frankfort', \ >'Louisiana': 'Baton Rouge', \ >'Maine': 'Augusta', 'Maryland': 'Annapolis', \ >'Massachusetts': 'Boston', \ >'Michigan': 'Lansing', 'Minnesota': 'Saint Paul', \ >'Mississippi': 'Jackson', \ >'Missouri': 'Jefferson City', 'Montana': 'Helena', \ >'Nebraska': 'Lincoln', \ >'Nevada': 'Carson City', 'New Hampshire': 'Concord', \ >'New Jersey': 'Trenton', \ >'New Mexico': 'Santa Fe', 'New York': 'Albany', \ >'North Carolina': 'Raleigh', \ >'North Dakota': 'Bismarck', 'Ohio': 'Columbus', \ >'Oklahoma': 'Oklahoma City', \ >'Oregon': 'Salem', 'Pennsylvania': 'Harrisburg', \ >'Rhode Island': 'Providence', \ >'South Carolina': 'Columbia', \ >'South Dakota': 'Pierre', 'Tennessee': 'Nashville', \ >'Texas': 'Austin', 'Utah': 'Salt Lake City', \ >'Vermont': 'Montpelier', \ >'Virginia': 'Richmond', 'Washington': 'Olympia', \ >'West Virginia': 'Charleston', \ >'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'} > > for k in capitals.keys(): > state = input('Enter the capital of '+k+' :') > if state.upper() == capitals[k].upper(): > right += 1 > print('Correct') > else: > wrong += 1 > print('Incorrect') > choice = input('Do you want to play again y/n: ') > if choice.upper() == 'N': > print('end of game') > break > elif choice.upper() != 'Y': > print("invalid choice") > > print('Number of correct answers is: ', right) > print("Number of incorrect answers is:", wrong) > > main() > > Regards, > Jakub ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a dictionary for capital quiz program
Thank you all for your help! I have a text today but I am not confident with this. So basically, what I did wrong was the indentation? Thanks Stephanie Quiles Sent from my iPhone > On Jun 2, 2015, at 10:15 AM, Peter Otten <__pete...@web.de> wrote: > > ZBUDNIEWEK. JAKUB wrote: > >> I'm a newbie, but was able to tune it to correctly reply to user inputs. > >> 2. Why (on Windows) do I have to give inputs in quotes not to cause an >> error (for ll input the error is ' NameError: name 'll' is not defined')? > > If you are running the script under Python 2 you should use > raw_input() instead of input(). input() will take the user input and also > run eval() on it: > > Python 2.7.6 (default, Mar 22 2014, 22:59:56) > [GCC 4.8.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. input("? ") > ? 1 + 1 > 2 raw_input("? ") > ? 1 + 1 > '1 + 1' > > Python 3 has no raw_input() and input() will behave like raw_input() in > Python 2. > >> 1. My question is can it be optimized in any way? > > In Python 2 capital.keys() builds a list. You can avoid that by iterating > over the dict directly: > > for k in capitals: >... > > Not an optimization, but if the user enters neither Y nor N you might ask > again instead of assuming Y. > > >> def main(): >> >>right = 0 >>wrong = 0 >>capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', "Arizona": >>'Phoenix', \ >> 'Arkansas': 'Little Rock', 'California': 'Sacramento', \ >> 'Colorado': 'Denver', 'Connecticut': 'Hartford', >> 'Delaware': 'Dover', \ 'Florida': 'Tallahassee', \ >> 'Georgia': 'Atlanta', 'Hawaii': 'Honolulu', \ >> 'Idaho': 'Boise', \ >> 'Illinois': 'Springfield', 'Indiana': 'Indianapolis', \ >> 'Iowa': 'Des Moines', \ >> 'Kansas': 'Topeka', 'Kentucky': 'Frankfort', \ >> 'Louisiana': 'Baton Rouge', \ >> 'Maine': 'Augusta', 'Maryland': 'Annapolis', \ >> 'Massachusetts': 'Boston', \ >> 'Michigan': 'Lansing', 'Minnesota': 'Saint Paul', \ >> 'Mississippi': 'Jackson', \ >> 'Missouri': 'Jefferson City', 'Montana': 'Helena', \ >> 'Nebraska': 'Lincoln', \ >> 'Nevada': 'Carson City', 'New Hampshire': 'Concord', \ >> 'New Jersey': 'Trenton', \ >> 'New Mexico': 'Santa Fe', 'New York': 'Albany', \ >> 'North Carolina': 'Raleigh', \ >> 'North Dakota': 'Bismarck', 'Ohio': 'Columbus', \ >> 'Oklahoma': 'Oklahoma City', \ >> 'Oregon': 'Salem', 'Pennsylvania': 'Harrisburg', \ >> 'Rhode Island': 'Providence', \ >> 'South Carolina': 'Columbia', \ >> 'South Dakota': 'Pierre', 'Tennessee': 'Nashville', \ >> 'Texas': 'Austin', 'Utah': 'Salt Lake City', \ >> 'Vermont': 'Montpelier', \ >> 'Virginia': 'Richmond', 'Washington': 'Olympia', \ >> 'West Virginia': 'Charleston', \ >> 'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'} >> >>for k in capitals.keys(): >>state = input('Enter the capital of '+k+' :') >>if state.upper() == capitals[k].upper(): >>right += 1 >>print('Correct') >>else: >>wrong += 1 >>print('Incorrect') >>choice = input('Do you want to play again y/n: ') >>if choice.upper() == 'N': >>print('end of game') >>break >>elif choice.upper() != 'Y': >>print("invalid choice") >> >>print('Number of correct answers is: ', right) >>print("Number of incorrect answers is:", wrong) >> >> main() >> >> Regards, >> Jakub > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a dictionary for capital quiz program
On 02/06/15 15:50, Stephanie Quiles wrote: > So basically, what I did wrong was the indentation? Yes. In Python indentation is all important. When you write a for (or while) loop Python executes all the indented code under the opening loop statement. When it sees an unindented statement it reads that as the next line to execute *after* the loop completes. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a dictionary for capital quiz program
On 02/06/15 15:15, Peter Otten wrote: Not an optimization, but if the user enters neither Y nor N you might ask again instead of assuming Y. He does. He only breaks if the user enters N choice = input('Do you want to play again y/n: ') if choice.upper() == 'N': print('end of game') break elif choice.upper() != 'Y': print("invalid choice") Y goes round again silently. Anything other than Y or N prints the error then tries again. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a dictionary for capital quiz program
On 02/06/15 09:45, ZBUDNIEWEK.JAKUB wrote: I'm a newbie, but was able to tune it to correctly reply to user inputs. 1. My question is can it be optimized in any way? Code can nearly always be optimised. Whether it is worth doing so depends on the need to do so. In this case I douybt its worthwhile :-) 2. Why (on Windows) do I have to give inputs in quotes not to cause > an error (for ll input the error is ' NameError: name 'll' is > not defined')? As Peter has said, you are probably running Python 2 rather than Python 3. input() changed behaviour in the upgrade. def main(): right = 0 wrong = 0 capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', "Arizona": 'Phoenix', \ 'Arkansas': 'Little Rock', 'California': 'Sacramento', \ You don't need the backslashes. Python is quite happy to read capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', "Arizona":'Phoenix', 'Arkansas': 'Little Rock', 'California': 'Sacramento', ...etc... } So long as its inside (),{}, or {} (or triple quotes, although they are slightly different) you don;t need line continuation marks (\). for k in capitals.keys(): state = input('Enter the capital of '+k+' :') if state.upper() == capitals[k].upper(): right += 1 print('Correct') else: wrong += 1 print('Incorrect') choice = input('Do you want to play again y/n: ') if choice.upper() == 'N': print('end of game') break elif choice.upper() != 'Y': print("invalid choice") print('Number of correct answers is: ', right) print("Number of incorrect answers is:", wrong) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a dictionary for capital quiz program
Alan Gauld wrote: > On 02/06/15 15:15, Peter Otten wrote: > >> Not an optimization, but if the user enters neither Y nor N you might ask >> again instead of assuming Y. > > He does. He only breaks if the user enters N > >>> choice = input('Do you want to play again y/n: ') >>> if choice.upper() == 'N': >>> print('end of game') >>> break >>> elif choice.upper() != 'Y': >>> print("invalid choice") > > Y goes round again silently. > Anything other than Y or N prints the error then tries again. ... with the next state. I meant that instead the question "Do you want to play again y/n:" should be repeated until there is a valid answer, either y or n. Current behaviour: $ python capitals.py Enter the capital of Mississippi :Jackson Correct Do you want to play again y/n: x invalid choice Enter the capital of Oklahoma : ... So "x" is a synonum for "n". Suggested behaviour: $ python capitals.py Enter the capital of Mississippi :Jackson Correct Do you want to play again y/n: x invalid choice Do you want to play again y/n: z invalid choice Do you want to play again y/n: n end of game ... ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a dictionary for capital quiz program
What is the +k+ called? How exactly does it work? I'm a big confused on that... Stephanie Quiles Sent from my iPhone > On Jun 2, 2015, at 12:17 PM, Peter Otten <__pete...@web.de> wrote: > > Alan Gauld wrote: > >>> On 02/06/15 15:15, Peter Otten wrote: >>> >>> Not an optimization, but if the user enters neither Y nor N you might ask >>> again instead of assuming Y. >> >> He does. He only breaks if the user enters N >> choice = input('Do you want to play again y/n: ') if choice.upper() == 'N': print('end of game') break elif choice.upper() != 'Y': print("invalid choice") >> >> Y goes round again silently. >> Anything other than Y or N prints the error then tries again. > > ... with the next state. I meant that instead the question "Do you want to > play again y/n:" should be repeated until there is a valid answer, either y > or n. > > Current behaviour: > > $ python capitals.py > Enter the capital of Mississippi :Jackson > Correct > Do you want to play again y/n: x > invalid choice > Enter the capital of Oklahoma : > ... > > So "x" is a synonum for "n". > > Suggested behaviour: > > $ python capitals.py > Enter the capital of Mississippi :Jackson > Correct > Do you want to play again y/n: x > invalid choice > Do you want to play again y/n: z > invalid choice > Do you want to play again y/n: n > end of game > ... > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a dictionary for capital quiz program
On 02/06/15 17:17, Peter Otten wrote: choice = input('Do you want to play again y/n: ') if choice.upper() == 'N': print('end of game') break elif choice.upper() != 'Y': print("invalid choice") Y goes round again silently. Anything other than Y or N prints the error then tries again. ... with the next state. I meant that instead the question "Do you want to play again y/n:" should be repeated until there is a valid answer, either y or n. OK, I agree that it's a rad unconventional. I thought you thought he was just quitting with the error message. Suggested behaviour: $ python capitals.py Enter the capital of Mississippi :Jackson Correct Do you want to play again y/n: x invalid choice Do you want to play again y/n: z invalid choice Yes this would be the normal idiom. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a dictionary for capital quiz program
On 02/06/15 17:25, Stephanie Quiles wrote: What is the +k+ called? How exactly does it work? I'm a big confused on that... You seem to be replying to the wrong post. I assume you mean this one from Joel? - >> for k in capitals.keys(): >> state = input('Enter the capital of '+k+' :') The above statement won't print the name of the capital. try something like this: state = input('Enter the capital of' + k + ':') -- The '+k+' isn't called anything per s. It's an example of string concatenation, and the only difference in Joel's rendition is the spacing, to clarify what's going on. (He may have misread your original code and thought it was in error?) Joel's version is easier to read, although it does lose some spaces in the prompt string. FWIW You could clarify it further by using better variable names such as: for state in capitals.keys(): guess = input('Enter the capital of ' + state +' :') Or you could have used string formatting: for state in capitals.keys(): guess = input('Enter the capital of %s:' % state) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] unittest with random population data
On 31/05/2015 03:00, Cameron Simpson wrote: On 30May2015 12:16, Sydney Shall wrote: Following advice from you generous people, I have chosen a project >that interests me, to develop some knowledge of python. My projest is a simulation of a biological population. I have a base class and a simulation function, which uses instances of the class. This, after many months of work and lots of advice, now seems to work well. It generates sensible data and when I write a small test program it gives sensible output. Now I want to learn to use unittest. I have written a unittest class which works OK. But the problem I have is that because I use the random module to populate my initial arrays, my data is not strictly predictable even though I am using seed(0). So the tests return many *fails* because the numbers are not exactly correct, although they are all rather close, consistent with the sigma value I have chosen for the spread of my population. I do of course use *almostEqual* and not *Equal*. First of all, several people have posted suggestions for getting identical results on every run. However, there is another approach, which you might consider. (And use in addition, not inseadt of, the reproducable suggestions). It is all very well to have a unit test that runs exactly the same with a test set of data - it lets you have confidence that algorithm changes do not change the outcome. But on for that data set. You say that your results are "all rather close, consistent with the sigma value I have chosen for the spread of my population". I would advocate making some "contraint" tests that verify this property for _any_ input data set. Then you can run with random and _changing_ input data sets to verify that your code produces the expected _kind_ of results with many data sets. So you would have one test which ran with a fixed data set which confirms preidctable unchanging results. And you have other tests with run with randomly chosen data and confirms that outcomes fall within the parameters you expect. You can apply those checks ("outcome in range") to both sets of tests. As an exmaple, I have a few classes which maintain data structures which are sensitive to boundary conditions. The glaring example is a numeric range class which stores contiguous ranges efficiently (a sequence of (low,high) pairs). It has a few add/remove operations which are meant to maintain that sequence on ordered minimal form. cutting and merging adjacent ranges is very easy to get wrong, very sensitive to off-by-one logic errors. So my tests for this class include some random tests which do random unpredictable add/remove operations, and run a consistency check on the object after each operation. This gives me good odds of exercising some tricky sequence which I have not considered explicitly myself. You can see the test suite here: https://bitbucket.org/cameron_simpson/css/src/tip/lib/python/cs/range_tests.py It has a bunch of explicit specific tests up the top, and then the random consistency test down the bottom as "test30random_set_equivalence". Cheers, Cameron Simpson MS-Word is Not a document exchange format - Jeff Goldberg http://www.goldmark.org/netrants/no-word/attach.html Cameron, Thanks for your most helpful reply. I have studied the material you indicated and it has been most helpful. I think that I have understood the principle involved, but I have had some problem implementing it. The range tests are mostly clear to me but there is one aspect I cannot follow. You use in this suite imports from Range, including Range, overlap, spans and Span. Are these programs that you have written? If so, are they specific to your set up or are they generic? If so, is it possible to obtain these programs? I have established a very primitive test suite based on your cs.range notions and it works fine, but it would be better, I am sure, to do it properly. Finally, I have one comment for the respected Moderator, if he is not out on a walk in the highlands in this cold and wet weather. I have taken the liberty of raising my problem here rather than elsewhere, because I have observed that the biological and bio-medical community, who always come late to new notions, is now rapidly discovering python. A great deal of work in these fields involve either stochastic simulations or statistical problems of analysis. The latter are more or less straight-forward, but the simulations are not. Thanks for all the help. You people are a model of how we could perhaps civilize humanity. -- Sydney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor