Our last lesson on functions was really long. We covered from basic functions to functions with keyword arguments, including functions with arguments, passing lists, default arguments, and more.
Now, it’s time to practice.
I’m gonna propose you a few exercises. Don’t worry, the logic to solve the problems is easy, as we are learning functions now. Logic will come later.
Open your IDE or terminal, read the question and once you understand what you have to do, try to solve it. Don’t get discouraged if you don’t know how to solve it 100%. Do your best. Google if you need. Google is going to be your best friend as a programmer.
Then, compare your solution with mine. It was
Challenge 1: Print a multiplication table
Create a function called ‘multiplication_table’. That function accepts one argument, an int. The function will print the multiplication table of that number, from 0 to 10.
For example. if you you pass ‘4’ as argument:
4 * 0 = 0 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36 4 * 10 = 40
Challenge 2: Find the average of all numbers in a list
Create a function called ‘average_list’ that accepts one argument: A list of numbers (int or float). Return the average of the list and print it.
Tip: Use *args.
Tip: The average is the sum of all elements, divided by the number of elements in the list.
For example, if we pass ‘ print(average_list(*[4, 6, 8, 2])) ‘, we should expect ‘5.0’
Challenge 3: Calculate the number of upper and lower case letters in a string
Create a function called ‘calculate_upper_lower_case’ and pass it a string as only argument. The function should print how many upper and lower cases are in the string.
You can check if a letter is upper case with .isupper(), islower() for lower case:
In [1]: lower_letter = 'a' In [2]: print(lower_letter.isupper()) False In [3]: upper_letter = 'A' In [4]: print(upper_letter.isupper()) True In [5]: print(upper_letter.islower()) False In [6]: print(lower_letter.islower()) True
Challenge 4: Print a city and its country
This one is simple. Create a ‘city_country’ function that prints the name of a city and its country. Here’s the catch: In case no arguments are passed ( ‘city_country()’ is called without arguments), print a default value anyway.
Call ‘city_country’ at least 4 times. On the first one, pass your city then your country. On the second one. your country and the city. The output should be the same as the first one. On the third, don’t pass any arguments. The fourth is up to you: Do what you want to do.
This function needs only 1 or 2 lines. Be smart about how to execute it.
Example:
city_country(city='Moscow', country='Russia') city_country(country='Russia', city='Moscow') city_country() city_country('Alger', 'Algeria') City: Moscow, Country: Russia City: Moscow, Country: Russia City: Alacant, Country: València City: Alger, Country: Algeria
Challenge 5: Final countdown
It’s the final countdown! Create a function that counts down from the number you pass (an int) to 0. But is not as easy. Use recursion to call the function itself instead of using a ‘for loop’.
final_countdown(5) 5! 4! 3! 2! 1! BOOOOOOOM!
That’s all! If you solved the 5 challenges, congratulations!
If you didn’t, congratulations! You did your best. Check the solution of the challenges you had trouble solving and after a few minutes, try to solve it again on your own.
Or create your own challenges and solve them!
Next lesson: 10 – Slicing
Link to the code: https://repl.it/@DavidMM1707/09-Function-challenges
Follow me on Twitter: https://twitter.com/DavidMM1707
My GitHub: https://github.com/david1707