CSCI1101:Assignment 3

Loops

Assigned: Tuesday, September 18
Due: Tuesday, September 25 at 11:59 PM

Print a table of numbers. Play a word-guessing game.




Chapter 7 from the Think Python book is relevant, but the best thing for preparation is to study the posted examples.

1. Print a table


(30 points for the basic version, 35 for the pretty version.)

Print a table whose first row consists of the integers 1 through 10, whose second is the square roots of those integers, whose third is the cube roots of those integers, and so on for five rows. Recall that you can compute the nth root of x using exponentiation:  x1/n .

One solution will print out

1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
1.0 1.4142135623730951 1.7320508075688772 2.0 2.23606797749979 2.449489742783178 2.6457513110645907 2.8284271247461903 3.0 3.1622776601683795
1.0 1.2599210498948732 1.4422495703074083 1.5874010519681994 1.7099759466766968 1.8171205928321397 1.912931182772389 2.0 2.080083823051904 2.154434690031884
1.0 1.189207115002721 1.3160740129524924 1.4142135623730951 1.4953487812212205 1.5650845800732873 1.6265765616977856 1.681792830507429 1.7320508075688772 1.7782794100389228
1.0 1.148698354997035 1.2457309396155174 1.3195079107728942 1.379729661461215 1.4309690811052556 1.4757731615945522 1.5157165665103982 1.5518455739153598 1.5848931924611136

which, let's face it, looks awful.  That's what my output usually looks like.  Try for a classier look, with neater columns and a tasteful number of digits after the decimal points:

   
  
   1.000   2.000   3.000   4.000   5.000   6.000   7.000   8.000   9.000  10.000

   1.000   1.414   1.732   2.000   2.236   2.449   2.646   2.828   3.000   3.162
   1.000   1.260   1.442   1.587   1.710   1.817   1.913   2.000   2.080   2.154
   1.000   1.189   1.316   1.414   1.495   1.565   1.627   1.682   1.732   1.778
   1.000   1.149   1.246   1.320   1.380   1.431   1.476   1.516   1.552   1.585

This is a bit of a cookbook problem, if you imitate the examples in the posted program nestedloops.py

2.  A Word-guessing Game


Download the linked files wordlist1000.txt and guessword.py, and put them in the folder in which you do the work for this course. 
 
The text file is a list of the 1000 most common words in English (well that's what I was promised, although it seems to contain only 999 words).  The function randomword()  in the .py file returns a word selected at random from this list.  You can see what it does by running the module and typing

test()

Do this several times.  The code for the function itself uses some stuff about reading from files that we haven't studied yet, but if you look at it closely, you may be able to figure out how it works, although this is not required.



(45 points) The function play(), which you will write, calls randomword()  (I've done this part for you already) and proceeds to play a guessing game with the user.   Here is a transcript of a typical session..

Try to guess the word I'm thinking of.
If the word you guess comes after my word
in the dictionary, I will say 'too far'.
If the word you guess comes before my word,
I will say, 'go farther'.
Enter your guess:

mother
go farther
Enter your guess:

sister
too far
Enter your guess:

p
too far
Enter your guess:

n
go farther
Enter your guess:

o
too far
Enter your guess:

neighbor
too far
Enter your guess:

nation
go farther
Enter your guess:

near
too far
Enter your guess:

navy
too far
Enter your guess:

national
go farther
Enter your guess:

nau
too far
Enter your guess:

nature
You got it! It took you  12  guesses.

Write your code in such a manner that no more than 20 guesses are permitted. In other words, if the user makes 20 incorrect guesses, the program terminates, and calls the user a loser, and prints the secret word. You really have to write just one rather carefully-crafted while statement (which of course will contain a number of statements in its body) to complete this.

 Needless to say, such a program should not actually print out the secret word at the outset.  However, you probably will want to see the secret word while you're working on your code, to make sure it is doing what it's supposed to.  For this reason, I've written the function with an optional parameter called
administrator, which by default is set to False.  If you just call play(), your program should play the game in normal mode, concealing the secret word from the user.  But if you call play(True), the value of this variable is set to True, and the program prints the secret word.  Think of this as 'administrator mode'.  

(20 points). You're not done.  This next part is an 'essay question'--actually it's a Computer Science question---so please submit a typed document in your folder, along with your programs. The question is this:  How many guesses should this take?  Of course, if you are really lucky, you might get the word on the first guess.  But what I'm asking for you to do is to describe a strategy that will   always reveal the answer in a relatively small number of steps. What if you had the list of one thousand words in front of you?  How would the answer change  if we only drew from a list of one hundred words?  or from a list of ten thousand or one hundred thousand words? Do not treat this as a throwaway question. Spend some time thinking about how long the guessing process takes if approached intelligently, and justify your answer.