Betel_Negash_Lab_02-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

10

Uploaded by CoachIronVulture32

Report
CS 171 Lab 2 Week 2 CS 171 - Lab 2 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. For questions 12 and 21 of this lab you must submit screen shots of your source code (.py file) What to submit: Lab sheet in PDF format to Gradescope Submission must be done via Gradescope Students’ Names: Betel Negash User IDs (abc123): bsn37 Possible Points: 88 Your score out of 88 : Lab Grade on 100% scale: Graded By (TA Signature):
CS 171 Lab 2 Week 2 Question 1: 7 points Execute each of the below Python statements. Write the output for each on the space provided. (a) (1 point) print (16 + 3) 19 (b) (1 point) print (16 3) 13 (c) (1 point) print (16 * 3) 48 (d) (1 point) print (16 ** 3) 4096 (e) (1 point) print (16 / 3) 5.33333333 (f) (1 point) print (16 // 3) 5 (g) (1 point) print (16 % 3) 1 Question 2: 7 points State the arithmetic operation each symbol represents. (a) (1 point) + Addition (b) (1 point) subtraction (c) (1 point) * multiplication (d) (1 point) ** exponent (e) (1 point) / division (f) (1 point) // floor division (g) (1 point) % remainder
CS 171 Lab 2 Week 2 FYI: An assignment statement is a line of code that uses a “ = ” sign. The statement stores the result of an operation performed on the right-hand side of the sign into the variable memory location on the left-hand side. Question 3: 2 points Enter and execute the following two lines of Python code: 1 2 (a) (1 point) What does the assignment statement: age = 15 do? Assign the value 15 to the variable age (b) (1 point) What happens if you replace the comma ( , ) in the print statement with a plus sign ( + ) and execute the code again? The code crashes because of a type error you can’t concatenate a string with an integer Question 4: 2 points Execute the following code. 1 answer = 6 ** 2 + 3 * 4 // 2 2 final = answer % 4 (a) (1 point) What is the value of answer after running the code? 42 (b) (1 point) What is the value of final after running the code? 2 Question 5: 2 points Execute the following code. 1 2 3 4 (a) (1 point) The third line of code contains an assignment statement. What is stored in the variable fullName when the line is executed? It contains the concatenated string “drexeluniversity” with no spaces (b) (1 point) How can you fix the output so that the words are separated? Add “” between school name and type of school so it would be fullName = schoolName + " " + typeOfSchool FYI: The “ + ” concatenates the two strings stored in the variables into one string. “ + ” can only be used when both operators are strings . age = 15 print (" Your age is " , age ) schoolName = "Drexel" typeOfSchool = "University" fullName = schoolName + typeOfSchool print (fullName)
CS 171 Lab 2 Week 2 Question 6: 2 points Execute the following code. 1 2 3 4 (a) (1 point) What happens when you execute the code? There isa type error because we are trying to concatenate an integer with a string directly (b) (1 point) How could you fix the code to work correctly? By converting the integer into a string using str() function so it would be streetAddress = str(addressNumber) + “ “ + streetName Question 7: 2 points Before entering the following code into the Python interpreter, try to figure out what you think the statement should print, then execute it. 1 myWord = " Hello! " * 10 2 print (myWord) (a) (1 point) What you think it does? I think it would print out the string yellow 10 Times (b) (1 point) What does it actually do? It printed out hello 10 times Question 8: 6 points Let’s take a look at a program that prompts the user for two numbers and subtracts them. 1 2 3 4 (a) (2 points) What happens when you execute the code? There is a syntax error because it tries to subtract to input strings with out converting them to integers (b) (2 points) Fix the program so it works as intended. What did you do? firstNumber = int( input ("Enter a number: “)) secondNumber = int( input (“Enter a number: “)) difference = firstNumber – secondNumber print ("Difference = ", difference) Added int before the input to convert user inputs to integers (c) (2 points) What does the int command do? It converts value to an integer addressNumber = 9701 streetName = "Germantown Ave" streetAddress = addressNumber + streetName print (streetAddress) firstNumber = input ("Enter a number: ") secondNumber = input (“Enter a number: ") difference = firstNumber – secondNumber print ("Difference = ", difference)
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