Python Workout 01 - Introduction to Functions

Overall Difficulty Rating:

Objective: This workout provides practice writing basic functions and calculations. Writing functions is an important skill to develop to simplify your code and avoid writing the same blocks of code over and over.

Problem 1: Write a function that takes in an integer, minutes, and converts it into seconds

Examples:

convert(5) ➞ 300

convert(3) ➞ 180

convert(2) ➞ 120

Problem 2: Write a function that takes two numbers as arguments and returns their sum. (Do not use the built in sum() function)

Examples:

make_sum(1,2) ➞ 3

make_sum(5,2) ➞ 7

make_sum(4,1) ➞ 5

Problem 3: Create a function that takes a number as an argument and returns “Fizz”, “Buzz” or “FizzBuzz”.

Instructions:

  1. If the number is a multiple of 3 the output should be “Fizz”.
  2. If the number given is a multiple of 5, the output should be “Buzz”.
  3. If the number given is a multiple of both 3 and 5, the output should be “FizzBuzz”.
  4. If the number is not a multiple of either 3 or 5, the number should be output on its own as shown in the examples below. The output should always be a string even if it is not a multiple of 3 or 5.

Submission

SImply post your code and a screenshot of your results.

Please format your Python code and blur it or place it in a hidden section.

This workout will be released on Monday March 27, 2023, and the author’s solution will be posted on Sunday April 2, 2023.

2 Likes
Summary

This text will be hidden

def convert(num_minutes):
num_type = type(num_minutes)
if (num_type != int):
result = “Please enter integer number”
else:
result = num_minutes*60
return result

def make_sum(num_one, num_two):
if (num_one is None or num_two is None):
print(“Please enter valid number(s)”)
return
return num_one + num_two

def make_fizzbuzz(num):
if (num % 3 == 0 and num % 5 == 0):
result = “FizzBuzz”
elif num % 3 == 0:
result = “Fizz”
elif num % 5 == 0:
result = “Buzz”
else:
result = str(num)
return result

2 Likes
1 Like

Great work @JordanSchnurman and @Neb

Hi, my submission:

def convert(num):
    if (type(num) != int):
        return("Please enter a valid number")
    else:
        return num * 60

def make_sum(num1, num2):
    return num1 + num2

def multiple(num):
    if ((num % 3 == 0) & (num % 5 == 0)):
        print("FizzBuzz")
    elif num % 5 == 0:
        print("Fizz")
    elif num % 3 == 0:
        print("Buzz")
    else:
        print(str(num))
1 Like

Here is my answer:

Problem 1

def minutes_to_seconds(minutes):
    '''This function converts minutes to integers'''

    #Define of object of minutes
    minutes_type = type(minutes)

    #minutes should be an integer or float
    if minutes_type == int or minutes_type == float:
        answer = minutes * 60
    else:
        answer = 'Please enter a number'
    return answer

seconds = minutes_to_seconds(45.5)
print(seconds)

Problem 2

# Problem 2: Write a function that takes two numbers as arguments and returns their sum. (Do not use the built in sum() function)

def make_sum(num1,num2):
    '''This function takes two numbers and returns the sum'''

    # The inputs cannot be None
    if num1 and num2 is None:
        answer = 'Please enter valid numbers'
    else:
        answer = num1 + num2
    return answer

nums = make_sum(2,6)
print(nums)

Problem 3

def fizzbuzz(number):
    '''This function solves the famous fizz buzz interview problem'''

    if number %3 == 0 and number %5 == 0:
        answer = 'FizzBuzz'
    elif number %3 == 0:
        answer = 'Fizz'
    elif number %5 == 0:
        answer = 'Buzz'
    return answer
def convert(x):
    if type(x) == int or type(x) == float: return x*60

def make_sum(a, b):
    return a + b

def Fizz(x):
    "Simple Function to return mods of 3 and 5"
    if x%3==0 and x%5==0: return "FizzBuzz"
    if x%3==0: return "Fizz"
    if x%5==0: return "Buzz"
    return x

My Submission for 1,2, and 3:

Summary
#Problem 1: Write a function that takes in an integer, minutes, and converts it into seconds
def minutes_to_seconds(minutes):
    if not isinstance(minutes, (int, float)):
        return "Please enter a valid integer or float number for minutes."
    return minutes * 60
print(minutes_to_seconds(5))   

#Problem 2: Write a function that takes two numbers as arguments and returns their sum. (Do not use the built in sum() function)
def sum_of_two_numbers(num1, num2):
    if not isinstance(num1, (int, float)) or not isinstance(num2, (int, float)):
        return "Please enter a valid integer or float number for both numbers."
    resultsp2 = num1 + num2
    return resultsp2
print(sum_of_two_numbers(5, 10))

#Problem 3: Create a function that takes a number as an argument and returns “Fizz”, “Buzz” or “FizzBuzz”.
def fizz_buzz(number):
    if number % 3 == 0 and number % 5 == 0:
        return "FizzBuzz"
    elif number % 3 == 0:
        return "Fizz"
    elif number % 5 == 0:
        return "Buzz"
    else:
        return str(number)
print(fizz_buzz(3))