Lab 6: Functions | CMSC 105 Elementary Programming - Fall 2024

Lab 6: Functions

Program 1: Calculate Discount

discount.py

''' 
Author name: <Your name here>
This program will input a price and discount rate and calculate the discount
'''

# Function to calculate a discount
def calculate_discount(price, discount_rate):
    return price * (discount_rate / 100)

# Input the price and discount rate from the user
price_input = eval(input("Enter price: "))
discount_rate_input = eval(input("Enter discount rate: "))

print("Discount is", calculate_discount(price_input, discount_rate_input))

Program 2: Divide By

divideby.py

''' 
Author name: <Your name here>
This program to read a number from the user and call the divBy5and6(num) 
function and printing the return value from the function.
'''

# Function takes as input a number and returns `True` if the number is divisible by 5. 
# It returns `False` otherwise.
def divBy5(num):
    return num % 5 == 0

# Function takes as input a number and returns `True` if the number is divisible by 6. 
# It returns `False` otherwise.
def divBy6(num):
    return num % 6 == 0

# Function takes as input a number num and calls `divBy5(num)` and `divBy6(num)`. 
# The function returns `True` if the output/returned values from both `divBy5(num)` 
# and `divBy6(num)` are `True`. It returns `False` otherwise.
def divBy5and6(num):
    return divBy5(num) and divBy6(num)

# Input a number from the user
number = eval(input("Enter a number: "))

print(divBy5and6(number))