Betel_Negash_Lab_06-AnswerSheet

.pdf

School

University of Maryland *

*We aren’t endorsed by this school

Course

171

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

18

Uploaded by CoachIronVulture32

Report
CS 171 - Lab 6 Professor Mark W. Boady and Professor Adelaida Medlock Content by Professor Lisa Olivieri of Chestnut Hill College Detailed instructions to the lab assignment are found in the following pages. Complete all the exercises and type your answers in the space provided. What to submit: Lab sheet in PDF. Submission must be done via Gradescope Please make sure you have marked the questions on the correct pages of the PDF and added any teammates to the submission We only accept submissions via Gradescope. Student Name: Betel Negash User ID (abc123): bsn37 Possible Points: 88 Your score out of 88 : Lab Grade on 100% scale: Graded By (TA Signature):
CS 171 Lab 6 Week 6 Question 1: 1 point FYI: PredeKined/built-in functions : segments of code already included in Python. print() , round() , abs() , pow() , int() are examples. Arguments : The information that a function needs to work. Arguments are sent to the function between the parentheses () . To use a function, call the function. input( " Enter your name " ) is a call to the input function sending the string " Enter your name " as an argument. Closely examine the Python program below. (a) (1 point) Circle / highlight calls to predefined functions in the program above. Question 2: 8 points Enter and execute the following Python program. (a) (1 point) What is printed by Line 2? 4.65 (b) (1point) What is printed by Line 3? 125 (c) (1 point) What is printed by Line 4? 7.0 (d) (1 point) What is printed by Line 5? 34 (e) (1 point) What is printed by Line 6? 7 (f) (1 point) What is printed by Line 9? 77 (g) (2 points) What is the difference between the round () function and the int () function? round() is a function to round a float value passed as a parameter, int() is a function that will cast a float value to an integer value and get leaves the decimal part by removing it. #Get Input name = input ("Enter your name: ") #Print Results print ("Name:", name) 1 #Sample functions 2 print ( abs ( 4.67) ) 3 print ( pow (5, 3) ) 4 print ( pow (49, 0.5) ) 5 print ( int (34.8) ) 6 print ( round (6.9) ) 7 8 import random 9 print (random.randint(1 ,100) ) 2
CS 171 Lab 6 Week 6 Question 3: 5 points What value is returned by each of the following function calls? (a) (1 point) abs (4.5) 4.5 (b) (1 point) int ("678") 678 (c) (1 point) round ( 5.6) -6 (d) (1 point) random.randint(4, 10) error because random is not deWined (e) (1 point) Is import random required to run random.randint(4, 10) ? Yes Yes No Question 4: 1 point Circle / highlight the argument in the call to the built-in function: The argument is the number wrapped inside round(). Question 5: 1 point answer = pow (4, 3) . What is/are the argument(s) in this code? 4 and 3 Question 6: 2 points If a function contains more than one argument, do you think the order of the arguments makes a difference? Explain your answer with an example. Yes, the order definitely matters. Take pow(4,3) for instance – that's 4^3, which gives you 64. Now, if you mix it up and go with pow(3,4), you're looking at 3^4, and that gives you a totally different result of 81. So, switching things around can change the whole thing Question 7: 6 points Execute the following lines of code: number = 45.78 answer = round (number) 3
CS 171 Lab 6 Week 6 (a) (2 points) Explain the purpose of the ceil() function The ceil() function rounds a number UP to the nearest integer, if necessary, and returns the result (b) (2 points) Explain the purpose of the floor() function. The floor() function rounds a number DOWN to the nearest integer, if necessary, and returns the result. (c) (2 points) Why are the calls to the floor() and ceil() functions preceded by “ math .”? You gotta import floor() and ceil() from the math module before using them. So, math comes first. Question 8: 6 points Review the following Code. import math x = 4.7 y = 5.3 z = 4.8 a = 3.2 print (math.ceil(x)) print (math.ceil(y)) print (math.ceil(z)) print (math.ceil(a)) print (math.floor(x)) print (math.floor(y)) print (math.floor(z)) print (math.floor(a)) FYI: A function is a segment of code that performs a single task. A function deKinition is the segment of code that tells the program what to do when the function is executed. The Wirst line of a function deWinition is known as the function header 4
CS 171 Lab 6 Week 6 (a) (1 point) What Python keyword is used to indicate that a code segment is a function deWinition? Def (b) (1 point) What are the two function headers in the Python code? def printMessage (): def main(): (c) (1 point) The name of the function is in the function header. What are the names of the two functions? printMessage and main (d) (1 point) Enter and execute the Python program. What is the output? Hello Programmer! Welcome to Python. Learn the power of functions! (e) (2 points) What line of code would you add to the program to print the last two lines twice? Where would you add the code? I would add printMessage () inside the function main() twice #Description: This program uses a function to print a message #Function Definition def printMessage (): print ("Welcome to Python.") print ("Learn the power of functions!") #Function Definition def main() : print ("Hello Programmer!") #Function Call printMessage () #Function Call main() 5
CS 171 Lab 6 Week 6 Question 9: 14 points Examine the following code. (a) (2 points) Label the function deKinitions and the function calls in the above code. Function definitions are: 1, area= math. pi * radius ** 2 print(“A circle of Radius %d has area %.2f”%(radius, area)) 2, radius = int(input(“Enter the radius: ”)) calculateArea(radius) main() is the function call. The computer will run what there are inside the main() function. (b) (2 points) The function call and the function deWinition for calculateArea each include a variable within the parentheses. The variable in the function call is known as an argument . The variable in the function deWinition is called a parameter . What is the parameter in the function deWinition? What is its purpose? A parameter is like a nametag for a variable that you toss into a function. It's how you bring in arguments to the party. (c) (2 points) In this example the parameter in the function de9inition and the argument in the function call have the same name. Is this required? #Description: This programuses functions to calculate # the area of a circle, given the radius import math def calculateArea (radius): area = math.pi radius ∗∗ 2 print ("A circle of Radius %d has area %.2f" %(radius, area)) def main() : radius = int ( input ("Enter the radius: ")) calculateArea(radius) #### Call to main #### main() 6
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help