05. Functions
05. Functions
When files are required, you can download them (here)[https://gitpub.pit-graduaten.be/en/783c79a4-2eaa-4a1a-baf8-ceef59ecb26b].
Functions without parameters
1. A simple calculation
Create a function called calculate() that calculates 15 * 8.
The function should return the result.
In the main program, call the function and print the returned value.
2. Calculate a price
Create a function called calculate_price() that calculates the price of a product.
The product costs €12.50 and you buy 4 of them. For now you can hardcode these values.
The function should return the total price, the main program should print the result.
3. Check a number
Create a function called check_number().
Inside the function, choose a number and determine whether it is even or odd.
Return "even" or "odd".
The main program should print a sentence containing the returned result.
4. Generate a username
Create a function called create_username().
Inside the function, define a first name and a last name.
Create a username by combining them in some sensible way, for example using lowercase letters.
Return the username.
The main program should print the returned username.
5. Calculate a rectangle
Create a function called rectangle_area().
Inside the function, define the width and height of a rectangle.
Calculate the area and return it.
Create a second function called rectangle_perimeter() that calculates and returns the perimeter of the same rectangle.
The main program should call both functions and print the results.
6. Functions calling functions
Create a function called get_number() that returns a number.
Create another function called calculate_square() that calls get_number(), calculates the square of the returned number, and returns the result.
Create a third function called describe_result() that calls calculate_square() and returns a sentence describing the result.
The main program should call describe_result() and print what it returns.
7. Simple score system
Create a function called get_score() that returns a score between 0 and 100.
Create another function called get_result() that calls get_score() and returns:
"Failed"for a score below 50"Passed"for a score from 50 to 69"Good"for a score from 70 to 84"Excellent"for a score of 85 or higher
The main program should print the returned result.
Try changing the score returned by get_score() to test all possibilities.
8. Temperature report
Create a function called get_temperature() that returns a temperature.
Create another function called get_temperature_description() that uses the returned temperature and returns a description such as:
"Freezing"for temperatures below 0"Cold"for 0–9"Mild"for 10–19"Warm"for 20–29"Hot"for 30 or higher
Create a third function called create_report() that calls the appropriate function(s) and returns a complete sentence containing the temperature and its description.
The main program should only print the result of create_report().
9. Shopping calculation
Create several functions for a small shopping calculation.
For example:
- one function returns the price of a product
- one function returns the quantity
- one function calculates the subtotal
- one function calculates a discount
- one function calculates the final price
Functions should call other functions where appropriate.
The main program should call one function that returns the final price and then print it.
Try to make the individual functions reusable. For example, the function calculating the discount should return a value rather than printing anything.
10. Mini text-based application
Create a small application of your choice, such as a number guessing game, ATM, quiz, restaurant order system, or character generator.
Structure the application using several functions.
Each function should return information rather than print it. Functions can call other functions and use their returned values.
For example, a function could return a player's score, another function could turn that score into a result, and another function could create a final message.
The main() function should control the program and handle all output.
Goal: If you wanted to use one of the functions later in a completely different program, you should be able to do so without having to remove print() statements from it.
Functions with parameters
1. Calculate the area
Create a function calculate_area() that receives the width and height of a rectangle as parameters.
Return the area of the rectangle.
The main program should call the function and print the result.
2. Full name
Create a function create_full_name() that receives a first name and a last name.
Return the two names combined into a single full name.
The main program should ask the user for both names and print the returned full name.
3. Even or odd
Create a function check_number() that receives a number.
Return "even" if the number is even and "odd" if it is odd.
The main program should ask the user for a number, call the function and print a sentence containing the result.
4. Calculate a price
Create a function calculate_total() that receives a price and a quantity.
Return the total price.
Create another function apply_discount() that receives a price and a discount percentage and returns the price after the discount.
The main program should use both functions to calculate and display the final price.
5. Temperature conversion
Create functions for converting temperatures:
celsius_to_fahrenheit()fahrenheit_to_celsius()
Each function should receive a temperature and return the converted temperature.
Ask the user which conversion they want to perform and the temperature to convert.
The main program should call the appropriate function and display the result.
6. Password validation
Create a function is_valid_password() with the following parameters:
- password: a string, to be checked, required
- minimum_characters: the minimum amount of characters, not required, default value of 8
- contains_number: boolean value, True indicates a number is required, not required, default value of True
- contains_special: boolean value, True indicates a special character is required, not required, default value of False
- contains_space: boolean value, True indicates a space is required, not required, default value of True
Return True if the password passes all the checks, otherwise, return False.
Create another function password_message() that receives the password, calls is_valid_password(), and returns a suitable message.
The main program should ask for a password and print the returned message.
7. Student result
Create a function calculate_average() that receives a bunch of scores in a list and returns their average.
Create another function get_result() that receives an average and returns:
"Failed"below 50"Passed"from 50 to 69"Good"from 70 to 84"Excellent"from 85 onwards
Create a third function create_student_report() that receives a student's name and three scores. It should use the other functions and return a complete sentence containing the student's name, average and result.
The main program should print the returned report.
8. Shopping cart
Create a function calculate_subtotal() that receives a list of prices and returns the total.
Create a function calculate_discount() that receives a subtotal and returns a discount based on these rules:
- below €50 → no discount
- €50–€99.99 → 5%
- €100–€199.99 → 10%
- €200 or more → 15%
Create a function calculate_total() that receives the subtotal and discount and returns the final price.
Finally, create a function create_receipt_total() that receives a list of prices and uses the other functions to return the final amount.
The main program should print the result.
9. Simple quiz
Create functions to build a small quiz.
For example:
check_answer()receives a user's answer and the correct answer and returnsTrueorFalse.calculate_score()receives the number of correct answers and the total number of questions and returns a percentage.get_result()receives the percentage and returns a description such as"Failed","Passed","Good"or"Excellent".
Let the main program ask several questions and keep track of the score.
At the end, use your functions to calculate and display the final result.
Remember: none of the functions should print anything.
10. Mini banking system
Create a small banking application using functions.
Create functions such as:
deposit()— receives the current balance and an amount, and returns the new balance.withdraw()— receives the current balance and an amount, and returns the new balance. Do not allow the balance to become negative.calculate_interest()— receives a balance and an interest rate, and returns the interest earned.add_interest()— receives a balance and an interest amount, and returns the new balance.create_balance_message()— receives a balance and returns a sentence describing it.
The main program should provide a simple menu allowing the user to deposit, withdraw or apply interest multiple times.
Keep the actual balance in the main program and pass it into the functions whenever it needs to be changed.
At the end, print the final balance and a suitable message.
Challenge: Try to design the functions so that none of them depend on variables outside the function. That way, they could be reused in another banking application later.