design, write and debug programs that accomplish specific goals, including controlling or simulating physical systems; solve problems by decomposing them into smaller parts.
use logical reasoning to explain how some simple algorithms work and to detect and correct errors in algorithms and programs
LO: Programming Challenge 1.
It is now time to use everything we have learned and put it together to create programs to solve problems like real programmers. These challenges will take longer than a single session and can be attempted individually or by working in pairs - remember, all ideas can be tried out using the IDLE shell and it's a good idea to use flow charts to help organise and understand what is happening in your program.
Before we begin it might be a good idea to look back at our suggestions for good programs:
Introduce the program - tell the user what the program does.
Explain how to use the program - what inputs the program will accept.
Hide the algorithim from the user - the user does not need to know how the program is working, they do not need to see anything apart from the output (the answer they want).
Present the answer in a readable way - if the user is expecting the answer in cm then they should recieve the answer in cm.
Remember you can also use the 6_game_loop_template.py available below - but don't use it for planning your program, that's what the flow charts are for.
Programming Challenge 1 - Question generator.
Teachers would like a program that will generate addition questions for them so they can include them in a test. The program needs to:
Generate 20 addition questions using 2 and 3 digit numbers.
Print out these questions including the question number (1 - 20).
Print out the answers under each question so the teachers don't have to work them out.
When designing your program you may have other ideas you may wish to include but these are the minimum requirements.
As this is the first challenge we will look into these requirements more closely and discuss how they could be achieved*:
Generate 20 addition questions using 2 and 3 digit numbers. This looks like we will need to use the randint() function to generate the 2 and 3 digit numbers and we will need to use a for loop to do this 20 times. This can be tested in the shell in IDLE.
Think, how you would import the randint() function?
Would you use one or two variables to hold the random numbers?
What is the smallest 2 digit number and what is the largest 3 digit number?
How would we write a loop that loops (iterates) 20 times?
Print out these questions including the question number (1 - 20). The question numbers could be the same number as the number of loops e.g. first loop = first question number, second loop = second question number...
How would we print the questions? What arguments would we include in the parameters of the print() function?
How would we ensure that the questions are presented in a readable way?
Print out the answers under each question so the teachers don't have to work them out. This looks like we would need another variable to reference the total of the two numbers...
How would we ensure that the answers are presented in a readable way?
Remember!!!! Do not try to write this program in one go! Make or draw some flowcharts to help organise your thinking. Test each idea in IDLE shell or write a small function and test it by writing a small file and running it.
Examples and demonstrations will be explored in 6.3b.
* A complete version** of this program is available in the code example section as 6_question_generator.py
** There is often several ways of solving a problem, the version in the file should not be seen as the 'correct' solution.