0% found this document useful (0 votes)
95 views3 pages

Lab 02

This lab exercise provides practice with selection and repetition control structures in Python. Students will work in pairs to create a program that takes integers as input and continues processing until 0 is entered. It will count and sum odd and even integers entered, ignoring negatives, and display the results. Completing and demonstrating the program to a TA is required to receive credit.

Uploaded by

jscansino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views3 pages

Lab 02

This lab exercise provides practice with selection and repetition control structures in Python. Students will work in pairs to create a program that takes integers as input and continues processing until 0 is entered. It will count and sum odd and even integers entered, ignoring negatives, and display the results. Completing and demonstrating the program to a TA is required to receive credit.

Uploaded by

jscansino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CSE 231 Lab Exercise #2

(Complete the Pre-Lab on D2L before your lab session)

Assignment Overview

This lab exercise provides practice with selection (if) and repetition (for, while) in Python.

You will work with a partner on this exercise during your lab session. Two people should work at one
computer. Occasionally switch the person who is typing. Talk to each other about what you are doing
and why so that both of you understand each step.

The if statement is used to choose between alternatives. The for and while statements are used to
repeatedly execute blocks of statements.

The for statement is most useful when you know exactly how many times you want to repeatedly do
something (for example, you know how many sides of a polygon you want to draw). Here is an
example showing how to print something n times:

n = 4
for i in range(n):
print("Hi!")

Here is the corresponding output:

Hi!
Hi!
Hi!
Hi!

The while statement is most useful when you don’t know in advance how many times the program will
be required to repeat a task (for example, prompting the user to enter a value until correct input is
provided). Here we will use while to repeat a fixed number of times simply to practice using the
statement. To compare and contrast with the previous example, we will print something n times:

n = 4
count = 0
while count < n:
print("Hi!")
count = count + 1

Here is the corresponding output:

Hi!
Hi!
Hi!
Hi!
In this example, note that you must manage the counter variable (“count”) yourself. If you make a
mistake with the counter management, the loop may end up repeating forever. In that case, in the upper
right of the IPython shell window is an icon with two choices, “Interrupt Kernel” and “Restart Kernel”.
Try “Interrupt Kernel” first and if that doesn’t work, “Restart Kernel” will.

Programming with Control Structures


Download the file “lab02.py” and develop a Python program which inputs a series of integers and
processes them. The program will:

a) Continue to process values until the user enters the value 0


b) Ignore all negative integers
c) Count the number of odd integers entered
d) Count the number of even integers entered
e) Calculate the sum of the odd integers in the series
f) Calculate the sum of the even integers in the series
g) Display the sum of odds
h) Display the sum of evens
i) Display the count of odds
j) Display the count of evens
k) Display the total number of positive integers entered
l) Optional: print a message whenever a negative integer is entered

Sample output:

Commentary

a) A nice characteristic of this problem is that it can be developed in small pieces. Remember:
always try to break a problem into smaller pieces that are easier to solve.

b) First write a program that prompts for an integer, displays the integer, and stops asking for more
integers when 0 is entered. Use while. Under what condition do you continue to loop? Here is a
suggested outline:
prompt for an integer # (and convert the string to an int)
while some_Boolean_condition:
# do something
prompt for another integer

c) Once that simple program is tested and working, add in another piece such as (c) to count the
number of odd integers entered. Create a variable with an appropriate name, such as
odd_count, and assign it an initial value of 0 (before the while loop). When an odd number is
entered, add one to odd_count (for example, odd_count += 1). Display the count.

d) Next: count the number of even integers and display the count.

e) Next: calculate the sum of the odd integers. The approach is similar to counting: choose a
variable name, initialize it to 0; when the integer is odd, add the integer to the variable. Display
the sum.

f) Next: calculate the sum of the even integers and display the sum.

g) Finally, ignore all negative numbers which the user inputs. One approach: if the integer is
positive, do all the counting and summing, else do nothing (that is, you can use an if statement
without an else clause).

h) If you have time, try this: print a message if the integer is negative.

 Demonstrate your completed program to your TA. On-line (Section 730) students should
submit the completed program (named “lab02.py”) for grading via the Mimir system.

Note (from the syllabus): Labs are credit/no-credit. To receive credit for a lab
1. You must complete the Pre-Lab, before your lab or the pre-lab deadline, whichever comes first. Pre-labs
are "warm-up" for the labs and are not expected to be perfect -- our expectation is necessarily fuzzy for
pre-labs: "you are expected to get most of them correct most of the time."
2. When Mimir tests exist, they test perfection. The Mimir tests allow you to verify that your code is correct.
However, you can get credit for correct Lab code that isn't perfect, i.e. it is possible to get credit for code
that fails Mimir tests. (Note that Projects are more strict with respect to Mimir tests.)
3. Adhering to the Coding Standard is expected, but expectations are less strict than for Projects.

You might also like