Reference no: EM132661588
Practical - Coding Checkpoint 2 & Strings
Use Docstrings
Please watch the checkpoint 2 video lecture on docstrings before starting this work.
For each and ALL of your functions from now on, write a brief function docstring.
Functions
Note that a program that uses a function means we need a main function as well.
A good general principle is that you should never write programs that use only one function.
If you're using a function (like those asked for below), then you also want a main.
If you only have main, then you don't really need it (but there's nothing wrong with a program that's entirely in a main).
1. Print a line
Write a function that prints a line of 40 hyphens.
2. Is it even?
Write a function to determine if an integer passed into it is even.
Note: In prac 6 we did this for if the number was odd, so it's nearly identical.
Don't copy that code unless you need help. Remember, we should know this now... so notice if you don't know how to do what you've already done before...
3. Get Non-empty String
Write a function to get a non-empty string. That is, you should be able to enter any string as long as it's not "".
Use this in a program to ask the user for their name using your function...
Then use the same function for getting their birthplace, and then display a message like:
4. Hi Martin from New Zealand!
So, if you need to use the same function for getting two different things and you want the user to know what you're asking, then you might need a function that can be customised... which is one thing we use parameters for.
Lists
4. Number List
Write a program that asks the user for a minimum and maximum number, then generates a list of integers between those two numbers, then prints it:
The maximum number must be greater than the minimum.
Note: Never use variable names that are the same as Python built-ins,
5. Subject List
Write a program to ask the user for their subject codes until they enter a blank one.
(Are you getting good at spotting words like 'until' and recognising that's the indefinite iteration pattern?)
All valid subject codes contain 6 characters, so reject any invalid subject code and ask again if needed.
When you have a list of subject codes, print them in sorted order and tell them how many subjects they have.
Then, tell them if they are cool or not... In the lecture notes, we determine if a student is cool based on whether they do CP1401 :)
All Together Now
IT JCU are expanding into the coffee business, and of course they need a Python program to help them, and they need you to write it...
This program includes lists, functions, decision structures, repetition structures and the usual input and output.
As always, the point is to apply what you have learned in terms of "best practice" (as best you know to date) and use the most appropriate code constructs to solve the problem.
We want a coffee-ordering program with a menu, a fixed list of valid coffees to check user orders against, and to print the instructions for making the selected coffee.
Of course we want to be able to drink our coffee (virtually), and because some people can't decide, we'd like to be able to choose randomly from the menu.
In the functions lecture, Barista Lindsay showed you how to make coffee using 4 'functions'. This program will use those... so make sure you've watched that video.
As always, start with planning.
As you look at the description and the sample output, think about:
• nouns (variables)
• verbs (processing, functions)
• sections, including repeated tasks (functions)
• types (strings, lists, etc.)
You should see that each of the functions in the coffee making video will be a function :)
You should remember that main should "look like the whole program", that is, main should generally consist of function calls (high level overview tasks), and
You should be able to clearly follow how the program runs by reading just the main function. You should recognise that when you display the coffees and when you check for a valid coffee order, you're working with a list of valid coffees
Note: The instructions are not meant to be complete or perfect. They're simplistic.
You should write simple functions for each step (like in the video). E.g.
Strings
Please make sure you have watched the Strings lecture before completing this section.
In that lecture, you were shown what you need to know for these exercises, including good examples very similar to these tasks.
6. Processing Strings
First, let's see another quick example of using slicing and finding.
Suppose we have data like:
string ="Result = 95%"
and we want to extract just the percentage as a number.
If we know the format will be just like that, we can use:
value =int(string[-3:-1])
But what if it might be different, like:
string ="Final Score = 8%"
# or
string ="Relative Value = 178.3%"
In those cases, our overly-simplistic approach would not work.
Ideally, we need a robust way of extracting the data, based on the possibilities the string could be.
So, in this case, let's assume we want all of the characters between the = and the % (which would be true in all 3 of these cases).
One way to do this is to find the index of the space after the = and then slice all the characters from there to one before the end.
Since we did this in the lecture, the rest is up to you now...
Write a program that processes this list of strings (copy the data here) and prints the values you extract.
data_strings= ["Result = 95%", "Final Score = 8%", "Relative Value = 178%",
"Something else that's very important = 9.2%", "x = 42%"]
7. Date Strings
(From the lecture)
Write a program to get and display only the year from a date of birth stored as a string like "13/07/1995"
Add a little interest to your program, by asking the user for their DOB, then printing like,
DOB: 13/07/1995
You were born in 1995
You turn/ed 25 in 2020.
8. Subject Code Strings
The first 3 characters in JCU Subject codes always follow a definite pattern:
The first 2 characters are the discipline (MA = Maths, CP = IT, etc.) and the next character is the year level.
Who knows what the last 3 characters mean? :)
Write a program that asks for subject codes until the user enters a blank one...
Hey, sounds familiar... just copy what you've already got, BUT this question does not use lists, so you have to change it!
After they type each one, print what year it is and whether it's an IT subject.
Re-Revision Practice
Note: We do NOT want you whacking in functions that are poor design -
e.g. violating SRP like a function that calculates and prints.
Also, remember to include a main function if you use any other function in your program.
When you're practising, always follow best practice!
Input, Processing, Output
1. Write a program that calculates a new value based on an original value and a (percentage-like) increase or decrease.
Example:
• Original: 100, Change: 0.05, Result: 105
• Original: 50.5, Change: -0.1, Result: 45.45
Decision Structures
2. Ask the user for the time of day (0-23 hours) until they enter a negative.
Then print a list of all the hours and how many of them were after noon:
(This program is quite different from checkpoint 1)
3. Coffee orders made simple.
• Ask the user for white or black coffee
• Ask for their chosen size: small, medium or large
• Then print the cost: For Black, Small = $3, Medium = $4, Large = $5. White coffee costs $1 more per size.
• If a user makes a mistake with their order, just pick the more expensive option :)
Repetition Structures
5. Write a program to ask the user for a low value and a high value, then print all of the integers between those values inclusive and show the total of those numbers.
Attachment:- Coding Checkpoint.rar