CS177 Fall11 - Lab for week: 5 Lab Title: Booleans, decision structures and loop structures Lab created by: Chenyun Dai, L. Martino 1. Learning objectives: In this lab you will learn how to make use of Boolean variables, logical operators and decision control operators. You will also learn how to write if statement, while statement and simple for statements for control flow. Specifically, you will learn: What are logical operators (and, or, not) Decision control operators (<, <=, >, >=, ==, !=) Simple and multi-way decisions While statement (loop) For statement (loop) Why to do that? Within an imperative programming language, a control flow statement is a statement whose execution results in a choice being made as to which of two or more paths should be followed. Every programming language has similar syntax or concept for control flow. It is very important for you to understand the concept and practice with examples. 1.1 RELATED MATERIAL Review of functions: Week4 recitation slides (http://wiki.cs.purdue.edu/177/_media/images:rec04.ppt) Booleans, Logic operators (and, or, not, and so forth), if statement, nested if statements, while loop: Week4 lesson slides (http://wiki.cs.purdue.edu/177/_media/images:week4v2.pptx) For statement: Week5 lesson slides, Textbook Chapter 2 Prelab material: prelab5: http://wiki.cs.purdue.edu/177/prelab5 2. SETUP instructions 1. Login into Windows (remember to use your Windows password!) During lab1, you should already have created the folder cs177 and the folder lab05. If not, perform the SETUP steps 2 to 6 described here: http://www.cs.purdue.edu/homes/lmartino/F11-CS177-supp-docs/setupinstructions.docx Then proceed to step 4 below 2. Click on the Windows icon, and then Computer. 3. Double click on the Network location icon showing \\lore.cs.purdue.edu\Your_username. 4. Download the skeleton file lab05.py under the lab05 folder you created. Your lab TA will guide you to perform the download. 5. Open the file lab05.py from the Python IDLE 3. Task(s) description: In this lab, you are going to write three functions, namely integerDecision, multipleThree and multipleThreeFive. integerDecision takes one input argument. It decides whether to print out the input number based on the criteria described in the following section. multipleThree also takes one input argument. It iterates over all the integers from 0 to the input argument. It has to find out which of the integers in the range (0, input argument] is a multiple of 3 (three) and prints out the total number of such integres. NOTE: the notation (0, somenumber] represent the numbers 1,2,3,…..somenumber. The notation [0, somenumber] represents the numbers 0,1,2,…somenumber multipleThreeFive is similar to multipleThree. However, it must print out a number if it is a multiple of three or it is a multiple of five. But it must NOT print out the number if it is a multiple of both three and five. NOTES: To facilitate your work, we provided you the file lab05.py that you have downloaded in the lab05 folder. This file contains some statements already provided to you and marked with the comment (remember that a line starting with the # sign is a comment!) # DO NOT MODIFY OR DELETE THIS STATEMENT Also statements following the comment: # DO NOT MODIFY THIS STATEMENT must not be modified. You have to write your Python statements in this file AFTER the # TODO comment. REMEMBER TO USE THE SAME INDENTATION! 4. Detailed description of your programs’ tasks. TODO 1 Write a function integerDecision. This function must have one input argument, number,. number is an integer number (examples: 100, -12), representing the number to be tested. HINT: use the names above when defining your function. The integerDecision function must test number by using the following logic: 1) If the number is less than or equal to zero, it prints “Input number is not positive.”. 2) If the number is less than 50, it prints “Input number is less than 50.” 3) If the number is greater than or equal to 50, and less than or equal to 100: a. If the number is NOT equal to 60, it prints the number itself (see the EXAMPLE below, case b)) b. Otherwise, it print “I FOUND 60.”. 4) If the number is greater than 100, it prints “Input number is greater than 100”. EXAMPLE: If you run the function with the following parameters: integerDecision(5), integerDecision(75) and integerDecision(101), integerDecision(-12), integerDecision(60), then the function must produce the following results: a) b) c) d) e) Input number is less than 50 Input number 75 is >= 50 and <=100 Input number is greater than 100 Input number is not positive. I FOUND 60. Specifically, you must write the following in the function: - TODO 1.01 Test if the input number is less than or equal to 0 - TODO 1.02 Test if the input number is less than 50 - TODO 1.03 Test if the input number is greater than or equal to 50, and less than or equal to 100 - TODO 1.04 Inside the condition of 1.03, test if the input number is equal to 60 - TODO 1.05 Test if the input number is greater than 100 → run the integerDecition function from the Python Shell. Try it with different values of the parameter. IMPORTANT: When you have finished writing your program in the file lab05.py, make sure to save it! IMPORTANT: when you have finished writing and testing the integerDecision function, turnin your lab05.py file. You can turnin your file several times. TODO2: Write a function multipleThree. This function must have one input argument, range1. range1 is an integer number (examples: 100, -5). The function must do the following: 1) If the input argument (range1) is less than or equal to zero, it outputs “Input number is not positive.” 2) If the input number (range1) is positive, then it tests all integer numbers in the range (0, range1] to see if they are multiple of 3 and returns the count of such integers, that is, the count of the integers that are multiple of 3 NOTES a) Saying that an integer can be divided exactly by 3 means that the remainder of its division by 3 is 0. Remind of the Python arithmetic operators! b) You are going to use the WHILE statement to implement the loop that tests all the integers in the range [0, range1]. Look at the Python range built-in function in Chapter 2 of the Textbook. - TODO 2.01 Test if the input number is less than or equal to 0. If so print out “Input number is not positive.” - TODO 2.02 If the input number is positive, write the while loop that iterates all the integers in (0, range1] with the proper exit condition. - TODO 2.03 Within the loop, test if the integer is a multiple of 3. If yes, print it and the count of the numbers that are multiple of three. See the examples below: The number 3 is a multiple of 3! Found 1 numbers that are multiple of 3! The number 6 is a multiple of 3! Found 2 numbers that are multiple of 3! Then, increment the count of such numbers. - TODO 2.04 Return the count. → run the multipleThree function from the Python Shell. Try it with different values of the parameters. IMPORTANT: when you have finished writing and testing the multipleThree function, turnin your lab05.py file. You can turnin your file several times. TODO3: Write a function multipleThreeFive. This function must have one input argument, range2. range2 is an integer number (examples: 100, -12), representing the upper bound of the integers to be tested (lower bound is fixed to 0). The function tests all integer number in (0, range2] by using the following logic: 1) If the input argument is less than or equal to zero, it outputs “Input number is not positive.”. 2) If the number is a multiple of 3 or a multiple of 5, it outputs the number. If the number is a multiple of 3 and 5, it will NOT print the number. See the following output example: >>> multipleThreeFive(15) 3 5 6 9 10 12 >>> Use the FOR statement to implement the loop. - TODO 3.01 Test if the input number is less than or equal to 0 - TODO 3.02 Write the for loop that iterates all integers in (0, range2] with the proper exit condition. - TODO 3.03 Within the loop, test if an integer is a multiple of 3 or 5. - TODO 3.04 Prints out the number as specified in point 2) above. IMPORTANT: when you have finished writing and testing the multipleThreeFive function, turnin your lab05.py file. You can turnin your file several times. TODO4 Write some code in the main() function. The function must do the following: TODO4.1 Get from the user the input arguments of: - the integerDecision function - the multipleThree function - the multipleThreeFive function TODO4.2 Call integerDecision, multipleThree and multipleThreeFive with the proper input argument. Print out the count of numbers returned by the function multipleThree. Example of the this print out: the count of numbers multiple of 3 is : 10 When you have finished writing your program in the file lab05.py, make sure to save it! 5. Test cases: When you have finished writing the main() function, you must run it with the different inputs provided below. Your lab TA instructor will explain you how to do it. We call these input “test cases”. A test case tells you which input values you must use and which is the corresponding output that your program must produce. This way you are able to see if your program implemented correctly the specifications. Why to run multiple test cases? Because, in general, your program may perform correctly for one test case, but may perform wrongly for other test cases. Case 1: INPUT VALUES: integerDecision: 50, multiple3: 30, multiple35: 20 EXPECTED OUTPUT: Input number 50 is >= 50 and <=100 The number 3 is a multiple of 3! The number 6 is a multiple of 3! The number 9 is a multiple of 3! The number 12 is a multiple of 3! The number 15 is a multiple of 3! The number 18 is a multiple of 3! The number 21 is a multiple of 3! The number 24 is a multiple of 3! The number 27 is a multiple of 3! The number 30 is a multiple of 3! the count of numbers multiple of 3 is : 10 3 5 6 9 10 12 18 Case 2: INPUT VALUES: integerDecision: 60, multiple3: 25, multiple35: 35 EXPECTED OUTPUT: I FOUND 60. The number 3 is a multiple of 3! The number 6 is a multiple of 3! The number 9 is a multiple of 3! The number 12 is a multiple of 3! The number 15 is a multiple of 3! The number 18 is a multiple of 3! The number 21 is a multiple of 3! The number 24 is a multiple of 3! the count of numbers multiple of 3 is : 8 3 5 6 9 10 12 18 20 21 24 25 27 33 6. Grading rubric TODO Points TODO 1.01 – 1.05 5 points each, total 25 points TODO 2.01 – 2.04 5 points each, total 20 points TODO 3.01 – 3.04 5 per each, total 20 points TODO 4.1 – 4.2 5 per each, total 10 points TOTAL 75 7. Turnin instructions Open a terminal window and login in your UNIX account by performing the Steps 1 through 3 described in the turnin-instructions document: http://www.cs.purdue.edu/homes/lmartino/F11-CS177-supp-docs/turnin-instructions.docx Then, in your UNIX terminal window, perform the following commands: $ cd $ cd CS177 $ cd lab05 $ turnin -v -c cs177=001 -p lab05 lab05.py The system should show you 3 lines like the following ones: tar: blocksize = 4 -rw-r----- 420/420 52 Aug 12 11:54 2011 lab05.py Your files have been submitted to cs177, lab05 for grading. The numbers appearing in the second line above may change (depending on the size of the file you turned in). However, CHECK that the number highlighted is NOT zero. If it is 0 you submitted an empty file!
© Copyright 2026 Paperzz