0% found this document useful (0 votes)
118 views17 pages

Charles Mathew Soriano LAB 4 and 5

This document provides a quiz on object-oriented programming using Python. It contains 12 critical thinking questions about while loops, loop structures, shortcut operators, and sentinel-controlled loops. The questions test understanding of initializing and updating variables in loop conditions, using operators like +=, and analyzing sample code involving user input and printing output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views17 pages

Charles Mathew Soriano LAB 4 and 5

This document provides a quiz on object-oriented programming using Python. It contains 12 critical thinking questions about while loops, loop structures, shortcut operators, and sentinel-controlled loops. The questions test understanding of initializing and updating variables in loop conditions, using operators like +=, and analyzing sample code involving user input and printing output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

OBJECT ORIENTED PROGRAMMING LANGUAGE USING PYTHON

1st sem. S.Y. 2021 – 2022


QUIZ 2

Name: Charles Mathew Soriano Score: _______ Date : 6/12/21


Section:Ceit 37 301p Schedule:(Time/Day)_______/______Instructor: Mrs Rowena

Laboratory 1: File Name lab05_reyes

Python Activity 2: Looping Structures – WHILE Loops


“Repeating code”
Learning Objectives
Students will be able to:
Content:
Explain the three parts of a loop
Explain the syntax of a while loop
Explain sentinel-controlled and counter controlled loops
Explain short-cut operators
Process:
Write code that includessentinel-controlled and counter controlled loops
Write code that uses short-cut operators
Prior Knowledge
Python concepts from Activities 1-7
Understanding of flowchart inputsymbols

Further Reading
Transitioning from Visual Logic to
Python: Chapter 8

Critical Thinking Questions


1. Closely examine the flowchart and Python program below.
ok
FYI: A looping structure allows a block of code to be repeated one or more times. A while loop is one of the two
looping structures available in Python.
Draw arrows between each flowchart symbol and the equivalent Python code.

a. In the Python code, circle all the code associated with the WHILE loop.
b. Enter and test the code. What does the line of code: x=x+1 do?
- It increase the value of the variable x by 1
c. How does the Python interpreter know what lines of code belong to the loop body?
-The lines of code belong to the loop is identifies by the (colon): symbol after the loop.

2. Every loop structure requires three actions. Identify the line of code in the Python program that corresponds to
each of the three actions. Initialize a variable used in the test condition:
________________________________________________________________________
a. Include a test condition that causes the loop to end when the condition is false:
- x = 0 initiates the variable which is used in the test conditionb.

B. Within the loop body, update the variable used in the test condition:

- x < 20 is the test condition that causes the loop stop when x value is equals or
greater than 20

Enter and execute the following code.


a. Beside each line of code below explain what the code does.

- The codes prints the numbers 10 per lines and it stops in the entered number.

b. Explain the difference between the two print statements.

- When a number is entered, it counts to five, but when a letter is entered, it becomes an error

3. The following code should print the numbers from 1 to 10, but it does not print anything.
Correct the problem.

- The output is

4. Enter and execute the following code: number = 0 while number <= 10: print(number)
number = number - 1

a. Describe the output. ____________________________________________________


b. What caused the output to be what it was? ______________________________________

c. Does the program end? Why or why not? _____________________________________

6. Enter and execute the following code:


number = 1 while number <= 10: if number % 2 == 0:
print(number, end= " ")
number = number + 1

a. State the output.


- Counting negative numbers indefinitely
b. What caused the output to display on one line?
- The number = number + 1
c. What control structures are used in this code?
- The while and if statement.

7. We want to create a program that prompts the user to enter a number between 1 and 10. As long as the
number is out of range the program re-prompts the user for a valid number. Complete the following steps to write
this code.

a. Write a line of code the prompts the user for number between 1 and 10.

________________________________________________________________________
b. Write a Boolean expression that tests the number the user entered by the code in step “a.” to determine if
it is not in range.
______________________________________________________________________

c. Use the Boolean expression created in step “b.” to write a while loop that executes when the user input is
out of range. The body of the loop should tell the user that they entered an invalid number and prompt
them for a valid number again.

________________________________________________________________________
________________________________________________________________________

d. Write a line of code that prints a message telling the user that they entered a valid number.

________________________________________________________________________
e. Put the segments of code from steps “a-d” together. Enter and execute the code. Does it work properly?
If not, correct it and test it again. _________________________

f. How many times does the loop execute? ______________________________________

FYI: A looping structure for which you know the number of times it will execute is known as a count-controlled
loop.

8. Sometimes a programmer does not know how many times data is to be entered. For example, suppose you
want to create a program that adds an unspecified amount of positive numbers entered by the user. The program
stops adding numbers when the user enters a zero or a negative number. Then the program prints the total.
Before creating this program, review the three actions required for all loops:

a. Initialize a variable that will be used in the test condition: What will be tested to determine if the loop is
executed or not? Write a line of code that initializes a variable to be used in the test condition of the loop
for this program. The variable should contain a value entered by the user.
________________________________________________________________________

b. Include a test condition that causes the loop to end when the condition is false: What is the test condition
for the while loop used in this program?
________________________________________________________________________

c. Within the loop body, update the variable used in the test condition: Write the code for the loop body.
Include the code to update the variable in the test condition.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________

d. Is this a count-controlled loop? Why or why not?


________________________________________________________________________

e. Complete the program. Enter and execute the code. Does it work properly? __________

FYI: Short-cut operators provide a concise way of creating assignment statements when the variable on the left
hand side of the assignment statement is also on the right-hand side. The addition short-cut operator (+=) is
usually used for incrementing a variable.
9. Enter and execute the following code: number = 1 number += 3 print(number)

a. What does the “+=” shortcut operator do?


________________________________________________________________________
b. The code: x += 5 is equivalent to which of the following lines of code?
x = 5 x = x +5
x=y+5y=x+5
c. Replace the operator ‘+=’ with the following shortcut operators and execute the code. Explain what each
operator does.

- =

* =
10. and execute the following code: bonus = 25 salary += bonus
print(“Total salary:”, salary)

a. What is the output of the preceding code? Is it what you expected?


- It was error. Yes, I was expecting it.

b. Edit the code so that it produces valid output.

-
c. As a result of correcting this code segment, what is needed in order for the shortcut operators to work
properly?
_______________________________________________________________________

d. the following line of code valid: 23 += total? Why or why not?


- Yes, it is valid due to Python's math problem solver.
11. The following code should print the numbers beginning at 100 and ending with 0. However it is
missing a line of code. Add the missing code, using the shortcut operator. Indicate where the code
belongs.

Here >>>> The missing code


_______________________________________________________________________
_ 12. Enter and execute the following code:

a. What does the program do?


________________________________________________________________________

b. What is the name of the variable used to store the user’s input? ____________________
c. In the print statement, what does word[0]represent? ___________________________ d. If
you changed 0 to 1 in word[0]in the print statement above, what is printed?
________________________________________________________________________
e. When does the program end? _____________________________________________

FYI: A sentinel-controlled while loop is a loop that repeats the loop body until the user enters a pre
specified value.

f. Why is the loop in this program is an example of a sentinel control loop?


________________________________________________________________________

g. Examine the print statement in this program:


print("First letter of " + word + " is " + word[0])
What is the purpose of the “+” as part of the argument in the print statement?
What happens if you replace the “+” with a “,”?
________________________________________________________________________
________________________________________________________________________
13. the code below.
name = “Simone” cost = 3.56
numApples = 89
What type of data is stored in each variable: (integer, floating point, or string)
name – __________________________
cost – __________________________
numApples – __________________________

FYI: A variable that can store only the values True and False is called a Boolean variable.

14. Given the assignment statement: foundCost = False


What type of data is stored in foundCost?

_____________________________________

Enter and execute the following code:

15. What value is stored in the variable foundCost?

a. What type of variable is ‘doAgain’?


-boolean
b. What does the program do?
- It determines which was the largest number entered by the user
c. What does the following line of code do?

maxNum1 = max(num1, num2, num3, num4)

d. Experiment with the arguments in the max() function in the program to determine if the function must
have four arguments. Demonstrate your answer.

e. What does the following code in the program do? if another != 'y':
doAgain = False
Application Questions: Use the Python Interpreter to check your work

1. Write a code segment that prompts the user for an even number. As long as the number is not even,
the user should be given a message and prompted again for an even number.
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

2. Write code segment that prompts the user for a letter from ‘a-z’. As long as the character is not
between ‘a-z’, the user should be given a message and prompted again for a letter between ‘a-z’.
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

3. Complete the following program:

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
OOP- Python Lec and Lab Activity

OBJECT ORIENTED PROGRAMMING LANGUAGE USING PYTHON


1st sem. S.Y. 2021 – 2022
QUIZ 2

Name: _____________________________________ Score: _________ Date : ___________


Section:______________Schedule:(Time/Day)_______/______Instructor: ________________

Laboratory 1: File Name lab06_reyes

Looping Structures: FOR Loops


“Another form of loops”
Learning Objectives Students will be able to:
Content:
Explain the difference between while loop and a FOR
loop
Explain the syntax of a FOR loop
Explain how to use the range() function in a FOR loop
Explain an accumulator in a FOR loop Process:
Write code that includes FOR loop
Write code that uses use FOR loops within functions
Prior Knowledge
Python concepts from Activities 1-8
Understanding of flowchart input symbols
Further Reading
Transitioning from Visual Logic to Python:
Chapter 9

Critical Thinking Questions:


1. Enter and execute the following two Python programs.
OOP- Python Lec and Lab Activity

a. What is the output for each program?


___________________________________________________________________________

b. Both programs produce the same output. Which code fragment is more concise?
-The second code because it’s much easier to determine how many variables
are visible in the output.

FYI: The Python predefined range() function is used to define a series of numbers and can be used in a FOR
loop to determine the number of times the loop is executed..

2. Enter and execute the following code fragments and state the output:
for x in range(5):
print(x, end=” “) = 0 1 2 3 4

for x in range(1,5):
print(x, end=" ") = 1 2 3 4

for x in range(3,20,2):
print(x, end=” “) = 3 5 7 9 11 13 15 17 19

numIterations = 6
for x in range(numIterations):
print(x, end=” “) = 0 1 2 3 4 5

OOP- Python Lec and Lab Activity


numIterations = 6
for x in range(1, numIterations+1):
print(x, end=” “) = 1 2 3 4 5 6_

3. After examining the five code fragments in #2, explain how the range() function works. Include an
explanation of the arguments.
- In the looping code, it collects all the numbers that fall within the specified range.
FYI: In a FOR loop you can include a list of values in place of the range() function.
________________________________________________________________________
________________________________________________________________________ 4.
Enter and execute the following code. for x in [3,6,9,12,15,18]: print(x, end=” “)
Output :

a. Rewrite this code using the range() function.

b. Why would you use the range() function when you could just list the numbers?

- It is the shortest and most efficient way to list the numbers between the start and end
numbers.

5. As you learned in Activity 2 , every loop structure requires three actions. Explain how these actions are
implemented in the following Python FOR loop. for x in range(1,5): print(x, end=" ")

OOP- Python Lec and Lab Activity


a. a variable used in the test condition;
________________________________________________________________________
b. Include a test condition that causes the loop to end when the condition is false:
________________________________________________________________________ c. Within
the loop body, update the variable used in the test condition:
________________________________________________________________________

6. Read through the code and determine what it does.


favorite = input("Enter your favorite ice cream flavor: ")
for x in range(1,5):
print(str(x) + “.”, favorite, end="\t")

a. what you think the program does.

- tt will display the four list of vanilla flavor

b. Enter and execute the code to determine if you were correct. What does the program actually do?
Provide a detailed explanation.
_____________________________________________________________________
_____________________________________________________________________

c. Explain the use of the str() function in the print statement. Why is it needed?
-To list all the different types of words

7. Complete the arguments in the following range function so that the code prints the even numbers
between 100 and 200 inclusive. for x in range(_____________________________): print(x)

8. the arguments in the following range function so that the code prints: 5 4 3 2 1 0. for x in
range(______________________________):

OOP- Python Lec and Lab Activity


print(x)
FYI: An accumulator is a variable that stores the sum of a group of values.

9. Examine the following code segment. total = 0 for x in range(5):


number = int(input("Enter a number: "))
total += number
print("The total is:",total)

a. Why is the variable total initialized to 0 in the first line of code?


- To identify which number will add up to 5

b. Explain what the following code does:


number = int(input("Enter a number: "))

- To command the user to input a number

c. Explain what the following code does: total += number

- to add integer and string

d. How many numbers does the program prompt for?

-2

e. What is the accumulator in the code segment?


- for x in range(5):

Is it better to use a FOR loop when you know the number of times the loop should be executed or when you do
not know? Explain your answer.
______________________________________________________________________________
______________________________________________________________________________

OOP- Python Lec and Lab Activity


Application Questions: Use the Python Interpreter to check your work

Write a code segment using a FOR loop that prints multiples of 5 from 5 to 500.
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Complete the program on the following page. The program should:
✓ Display five addition facts, one at a time, and allow the user to answer them.
✓ Provide the correct answer if user enters incorrect answer.
✓ Print a congratulatory answer, if the answer is correct.
✓ Keep track of the number of problems the user answers correctly.
✓ Prints a special message, if the user gets all five problems correct.
Fill in the missing code; comments (also written below) indicate location of missing code.

# include the required import statement

# include a FOR loop that prints the numbers from 10 to 0

# Assign a random number between 1 and 10 to num1 and num2

# Write the test condition to determine if the user’s


# answer is equal to the sum of the two numbers
______________________________________________________________________________

OOP- Python Lec and Lab Activity


# Write the line of code to increment the variable
# that is keeping track of the number of correct answers.
______________________________________________________________________________
# Print the addition fact and include the correct answer.
______________________________________________________________________________

Python Activity 3: Looping Structures: FOR Loops

# Write the test condition to determine if the user answered


# all the questions correctly
______________________________________________________________________________

# Write the code to call the function that prints the rocket
______________________________________________________________________________

OOP- Python Lec and Lab Activity


OOP- Python Lec and Lab Activity

You might also like