An “IPO” problem involves Input, Processing, and Output.
The inputs are the parameters of your function. The processing is whatever commands your function does, and the output is the value returned by your function.
In each problem below, you write a function which will be called later.
A couple of notes:
- Do not print anything inside your function. Only return the value.
- Do not use the input() command inside the function. The inputs come from the parameter variables. Any input would happen in the main body of the program.
Here’s an easy example:s
Write a function that calculates and returns the area of a square. The function should be called square_area and it should take a single parameter called side.
Solution:
def square_area(side):
area = side * side
return area
Notice where the parameter variable side appears in parentheses in the first line of the function. This first line is called the signature.
Here are a few ways how this function could be called. To call a function means to jump to that function’s commands, run them, then return back to the line that called to the function. At that point, the function becomes equal to whatever value was returned.
def square_area(side):
area = side * side
return area
print(square_area(3.2))
area1 = square_area(19.2)
area2 = square_area(205)
print(area1)
print(area2)
your_side = float(input('Enter side length: '))
your_area = square_area(your_side)
print("Your square's area:", your_area)
Notice that the square_area function can take any number as its input.
When we send a value into a function to use as its parameter, we say that this value is passed into the function. Also, this value is called an argument. So, the argument is the actual value that gets passed in, whereas the parameter is the variable that is used inside the function, storing and processing any argument that is passed.
Some of the calls in the example above pass a literal number as the argument, while others pass a variable. It makes no difference to the code inside the function as long as a number arrives.
In the main body of the program (i.e. the code that is not inside a function), the input() function is used to get user input. This code does not belong inside the function in this case. The function’s job is only to calculate areas of squares, not to gather user input.
The square_area function doesn’t care if the side value comes from a literal, or a from a variable, or from some other function like input(). This is a good thing – it makes the function useful in a wider variety of situations.
Practice Problems
1. Write a function that takes two strings as parameters and returns the longest one. In case of a tie, return the first string.
# Starter Code (copy and paste)
def longer_string(string1, string2):
# your code here
# return a value
# Do not change any code below
s1 = input('Enter a string:')
s2 = input('Enter another string:')
result = longer_string(s1, s2)
print('The longest string is:', result)
2. Write a function named total_price that takes the quantity of items as a parameter named qty and returns the total price. Each item costs 10. If the customer buys 5 or more, each item is discounted to 9. Remember to test your code with different input values to see if it works correctly.
# Write your function here
# Do not change any code below
q = int(input('Enter the quantity:'))
result = total_price(q)
print('The total price is:', result)
3. Write a function named count_vowels that takes a string as a parameter and returns the number of vowels (a, e, i, o, u) in the string. (Hint: use a loop and check if each character is in “aeiou”)
After writing the function, write your own code to test it with different input values. For example, you might try “hello”, “banana”, and “xyz”.