How to write a computer program : An example

I am going to write a program iin Python that will ask the user to find a word that rhmes with one given by the program. This web page will show, and discuss, how I do it.

I recommend writing and testing each part separately.   Try to break your program into separate parts that can coded and tested separately, the smaller the better. You may need to make some changes when you combine them, but you can test the main things in each piece separately. When you combine them, do it in stages, add in as little as possible, then get that combination to run error free.

.   Another thing I recommend is that each time you have a working program that you plan to add to, save it and then save it again with a new number appended to it. That way, if you mess up the new one too badly, you can go back to the previouse one. When I was working on the program to find prime numbers I had : prime_1.py,   prime_2.py,    prime_3.py,      etc.   

Here we go with my approach to writing a program:

Step 1 is to build a rough mental model of how the program will work. You can do this in words, pseudo code, or a diagram (also known as a "flow chart")

Description of how program will work:

The progam will define a word for rhyming. I plan to use "blue". I will put a list of words that rhyme with blue in a list in the program. . It does not have to be complete because this is just a sample game. The program will then use a loop to ask the user to enter a word that he or she thinks rhymes with "blue" and check to see if it is in the list of rhyming words. If it is in the list, the program will tell the user he or she "won" and then end.

Psuedo Code:

define the word for ryminng as "blue"

Enter a list of words that ryme with blue

Start a loop:

General Approach to creating the program:

My first step will be to write a short program gets a guess from the user. I want to get this working before I write the program that will use it.

My next step will be create the full program.

[ NOTE: when working with the Python IDLE system, you may find it useful to size and place the "Shell" window and the program window side by side . It is also useful to have quick access to the "Examples" web site.  Click here for a screen catpture that shows an example of this ]

Once the program is working I will think about how I could make ir more interesting and decide what to do next. Actualy I have already been thinking about how to make it more interesting, but I'm not going to use any of those ideas untill I get the simple program working.

Short working Program to get a guess from user:           click here to see the program below in separate window

                                                                                     Note: This program works with "string" (i.e. text, not number) input from the user.
                                                                                               Your number guessing program will need numbers, not strings. Click here for a program that will get numbers from user input

#HOW_TO_1.PY Get user input )
                

import random
import sys
import os

#prompt the user to enter something:
print("Enter a word : ", end="")

#get value from user
guess = sys.stdin.readline()

print("\nYou entered ; ",guess)

#to remove the trailing "\n", use rstrip()
guess = guess.rstrip()

 Full working Program : ( click here to see program in separate window)

#HOW_TO_1.PY Get user input

import random
import sys
import os

#define word for ryming

target = "blue"

#create a list of ryming words:
#Google found 610 words...
rhymer_list = [
"queue","flu","bleu","lieu","through",
"rew","flue","blew","liu","thru",
"rhew","foo","boo","loo","to",
"rhue","frew","brew","lue","too",
"rue","glew","brue","mew","true",
"schewe","glue","chew","moo","tu",
"schoo","gnu","chiu","mu","two",
"screw","goo","clue","new","u",
"shoe","grew","crew","nu","u.",
"shoo","hew","cue","ooh","uwe",
"shrew","hewe","deux","oooh","view",
"sioux","hoo","dew","pew","vous",
"skew","hue","do","phew","whew",
"slew","jew","doo","poo","who",
"soo","jus","drew","pooh","woo",
"spew","kew","drough","pshew","wu",
"sprew","knew","due","pugh","yew",
"stew","koo","ewe","q","yoo",
"strew","leu","few","que","yue",
"threw","lew","flew","zue","zhou",
"zhu","zoo"
]
end_loop = "False"

while (end_loop == "False"):

#prompt the user to enter something:
print("Enter a word that rhymes with ",target, " : ", end="")

#get value from user
guess = sys.stdin.readline()

print("\nYou entered ; ",guess)

#to remove the trailing "\n", use rstrip()
guess = guess.rstrip()

if ( guess in rhymer_list ):
       end_loop = "True"

print ("\nCongratulations !,", guess, "was in our list of words that rhyme with ",target,"\n")

 

OK. That's my initial program.     To make it more interesting I could modify it to let the user keep guessing words untill they missed one, or decided that they were ready to quit.

Each time around the loop with a correct answer the program would include code like this:

print("Yes! ", guess, " is also in our list of words that rhyme with ", target, ". You now have guessed ",count," correct words !\n")

print("Would you like to try again? : ",end="")

# program would then get an answer and test to see if it was "yes" or "no" and respond appropriately