Selection - Tom Kleen



Python Practice Test #2SelectionWrite a function called classification that accepts one argument: an integer representing a person's age. Examine the number and return a string based on the following rules. If the age is less than 5, return "Preschool". If the age is from 5-18 (including both), return "School". If the age is from 19-23 (including both), return "College". And if the age is 24 or higher, return "Adult". print("Should be 'Preschool':",classification(4)) print("Should be 'School':", classification(6)) print("Should be 'School':", classification(18)) print("Should be 'College':", classification(19)) print("Should be 'College':", classification(23)) print("Should be 'Adult':", classification(24))ListsWrite a function called getNames that will accept two arguments: (1) a list of names (strings), and (2) a single letter (string). It will return a list whose elements are all of the names from the original list whose name begins with the letter that was passed in. names = ["Ann", "Bob", "Chuck", "Bill", "Dave", "Betty", "Carla", "bret", 'carrie'] print("Should be 4 names:", getNames(names, 'b')) print("Should be 4 names:", getNames(names, 'B')) print(" Should be 1 name:", getNames(names, 'D')) print("Should be 0 names:", getNames(names, 'X'))StringsWrite a function called getFirstName that accepts a single string as its argument. The string will look like this: "lastname, firstname" (a last name, followed by a comma and a space, followed by the first name). Your function should return the first name. There should be no characters before the first letter of the first name (that is, do not return the space or the comma before the first name). print("Should be 'Martin':", getFirstName("Van Buren, Martin")) print("Should be 'Abraham':", getFirstName("Lincoln, Abraham")) print("Should be 'Bobbi Jo':", getFirstName("Bradley, Bobbi Jo"))LoopsWrite code that asks the user to enter two numbers called low and high. After that, repeatedly ask the user to input an integer. If it is between low and high (including both) add it to running total. When the user finally enters the number 0, quit asking for numbers, print the sum. Below is the input and output from a session where the user entered 100 for low and 200 for high.Enter low: 100Enter high: 200Enter a number:100Enter a number:99Enter a number:200Enter a number:201Enter a number:0Sum is: 300 ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download