11.6 Practice


Count Magical

Practice: Write a function called count_magical that returns the number of even numbers in a given list. In the function, if the number of evens is greater than half of the length of the list, print "Magical". Else, print "Not Magical". Write a function called main which tests the count_magical function on at least 3  different lists of integers. Use the main function to test count_magical by calling main().

Cashier Job

Practice: Write a function called calculate_total that will take the number of pennies, nickels, dimes, quarters, and discount rate (i.e. 15 for 15% discount). Return the total amount of money after discount. Print what is returned by the function after it is run with 97 pennies, 13 nickels, 18 dimes, 54 quarters, and 20% discount. Print what is returned by the function after it is run with 32 pennies, 19 nickels, 22 dimes, 71 quarters, and 51% discount.

Number Mystery 1

Practice: Write a function called num_mystery that takes in 3 integers. The function should calculate the sum of the 3 integers and the difference between the largest integer and the smallest integer. The function should return the product of these two integers you calculated. Hint: You may find it useful to use the max() and min() functions. Use the num_mystery function on 1, 2, 3 and print the result. Use the num_mystery function on 5, 13, 7 and print the result.

Remove Duplicates

Practice: Write a function called remove_duplicates. The sole parameter of the function should be a list. The function should go through a list, find all duplicate elements, and remove them. Sort the resulting list. YOU MAY NOT USE the set() function in Python. Use [1, 1, 2, 5, 4, 6, 12, 3, 4, 6] as an example list to input into your function.


Practice: Display the string "Apple" in the following formats:
1) normally
2) all uppercase
3) all lowercase
Display the string "mRoWiE" in the same 3 formats. Ask the user to input a sentence, and display this input in the same 3 formats. Do this in AT MOST 8 lines of code. By the end of the program, 9 lines should have been displayed (3 formats for each of the 3 strings). Example of the 3 formats for one string: AppleAPPLEapple


Practice:  Write a function that takes a list of numbers as input and returns the product of all the numbers in the list. Use it to print the products of the following sets of numbers: -1, 5, 3, 2, 82.5, 3, 04, 3, 7, 10


Practice:  Write a function that takes in 2 integer parameters: length, and width. The function should print out a rectangle of asterisks (*) with that length and width. Example, if the length is 5 and the width is 3, the function should print:
  • Useful information:
    • 1) print("a", end="") removes the newline from the end of the print statement.
      2) print("a" * 5) displays "aaaaa".
Use the function to display rectangles with the following dimensions (with a linebreak between each one): 2 x 67 x 43 x 5


Practice: Code a function named shopping that will print the number of items that a customer would like to buy. It will take in an unknown number of parameters, each representing one item. After that, the function will ask the customer, "Are you sure you would like to buy " and stating the number of items the customer wants to purchase. Your function does not need to respond to a yes/no answer from the user; it just needs to print the output (the question).
Example 1:
Parameters: "soap", "brush", "comb"
Output: Are you sure you would like to buy 3 items?
Example 2:
Parameters: "lotion", "shoes", "pencil", "crayon"
Output: Are you sure you would like to buy 4 items?

Nutrition Facts

Practice: Write a function that provides the nutrition facts of an item within the provided dictionary nutrition_facts. It should provide the calories by default. It should accept the other keys within the item's dictionary as keyword arguments. Use **. If that keyword argument is True, then print out the value stored by the key in addition to the default string that says the number of calories. If the user entered in an invalid specific, it should tell the user about this. If the user entered in an invalid food, it should ignore the user completely.
Example 1:
Parameters: "lays potato chips", allergens=True
Output: Lays potato chips have/has 220 calries
"allergens": ["processed on equipment that also processes peanuts", "contains milk ingredients"]
Example 2:
Parameters: "lays potato chips"
Output: Lays potato chips have/has 220 calories
Example 3:
Parameters: "lays potato chips", main_ingredients=True
Output: Lays potato chips have/has 220 calories
"main_ingredients": ["potato", "salt", "canola oil"]
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.