For Project 4, we'll be creating a Dice Roller program in Python.
You'll continue to learn more concepts in Python as well as reinforce some of the software development steps from the previous project.
By the end of this project, you will have gained valuable experience and foundational skills that will be useful for future projects:
Master the basics of creating and using functions in Python.
Learn about parameters and arguments in the context of functions.
Become proficient in using 'try' and 'except' for error handling.
Develop skills in breaking down complex tasks into smaller ones.
These skills are essential for anyone starting in programming and will help you tackle more complex projects in the future.
Listen to Alex below, he has a program he wants you to build. If you're still not sure afterwards, you can chat with him using the chatbot below.
📖 Afterwards, write down four requirements into your workbook, in plain-English, that you think Alex wants you to include in the program.
An example of this might be: "The program should greet the user and explain that it is a Dice Rolling App".
📖 In your workbook create an IPO Diagram for the program
Remember, you need to:
Determine the inputs and outputs
Describe the processing in a clear, non-ambiguous way
Modularity is the practice of breaking down a program into smaller, independent components or modules. These modules usually serve a distinct function and can operate independently of each other. The key advantages of modularity are reusability, ease of maintenance, and improved readability. Reusable modules can be employed in different parts of a program or even in entirely different projects, saving both time and effort. When each module is responsible for a specific function, it becomes easier to debug, update, or enhance that particular function without affecting other parts of the system.
Below is a simplified guessing game, written in pseudocode. We can simplify this program further by breaking it down into smaller problems.
When dealing with either code or pseudocode, the first step towards achieving modularity is to identify tasks or operations that are either repeated frequently or can be isolated as independent functions. Once these tasks are identified, they can be encapsulated into functions or sub-programs. For example, in a guessing game, tasks like generating a random number, capturing user input, and providing feedback can all be isolated into separate functions. By doing so, the main program becomes a simpler structure that calls upon these functions, making the code more organised and easier to understand.
randomNumber = random number between 1 and 100
WHILE true
PRINT "Guess a number between 1 and 100"
userGuess = READ user input
IF userGuess > randomNumber
PRINT "Too high"
ELSE IF userGuess < randomNumber
PRINT "Too low"
ELSE
PRINT "Correct"
BREAK
⟶
Here we write the pseudocode for a 'main' function:
Much of the existing code has been extracted into separate functions.
Main Program:
randomNumber = generateRandom()
WHILE true
userGuess = getNumber()
feedback = printFeedback(userGuess, randomNumber)
PRINT feedback
IF feedback == "Correct"
BREAK
The pseudocode for these functions would then also need to be written:
generateRandom:
RETURN random number 1-100
getNumber:
PRINT "Guess a number between 1 and 100"
userGuess = READ user input
RETURN userGuess
printFeedback:
IF userGuess > randomNumber
RETURN "Too high"
ELSE IF userGuess < randomNumber
RETURN "Too low"
ELSE
RETURN "Correct"
Structure charts provide a visual representation of the program’s architecture, showcasing how different modules interact with each other. They are hierarchical diagrams where the main program is at the top and the modules and sub-modules branch out below. Lines connecting these elements represent the flow of data or control between them. Structure charts are incredibly useful for both planning and understanding a program. During the planning phase, they help in breaking down the program into smaller modules, encouraging a modular approach to coding. In terms of understanding existing programs, a structure chart offers a quick, visual way to comprehend how different components interrelate, making it easier to debug, maintain, or expand the program.
📖 In your workbook, complete the structure chart activity.
Open the GitHub Classroom assignment, then complete the labs and project below:
Learn how to create functions in Python
Get set up on GitHub with Project 4
Lab 2 - Parameters and Arguments
Learn how to define parameters in functions
Learn how to pass arguments to functions
Learn about try/except to handle errors gracefully
Less Comfortable:
Use at least two functions
At least one function should return a value
At least one function should use a parameter
Exception handling
More Comfortable
Input validation
Use at least three functions
Ability to retry or exit
Functional Testing involves verifying that individual components or functions of the software work as intended, often by using a testing table to compare actual outputs against expected outputs for given inputs. This is what we have done in previous projects.
Acceptance Testing assesses if the entire application fulfils the predefined business requirements and user stories, focusing on overall system behaviour to ensure it is fit for real-world usage by end-users. For this project we aree going to explore this technique.
📖 Complete the functional testing and acceptance testing activities in your workbook.
Go back through your program and ensure it is maintainable by using code commenting and meaningful variable names.