HIMT 345
Homework 06: Iteration
Overview: Examine PyCharm’s “Introduction to Python” samples for
Loops. Use PyCharm to work along with a worked exercise from
Chapter 5. Use two different looping strategies for different
purposes.
Prior Task Completion:
1. Read Chapter 05.
2. View Chapter 05’s video notes.
3. As you view the video, work along with each code sample in
PyCharm using the Ch 5 Iteration PPT Code Samples.zip.
Important: Do not skip the process of following along with the
videos. It is a very important part of the learning process!
Specifics: PyCharm’s “Introduction to Python” project contains
several examples of iterations or looping (see list at right).
Follow the instructions to complete them. (Not handed in.)
Use PyCharm to work along with the video solution for Exercise 5.1
from the textbook. Type in the statements and execute the program
as is done in the video. [Create a project and save your work as
“Worked_Exercise_5.1”. It will prove valuable for part a below.]
(Not handed in.)
Create a new PyCharm project called “Hwk06_<your last name>”.
Create two new python files within that project called
“Hwk06a_<your last name>” and “Hwk06b_<your last
name>”.
Part (a): Let’s write the code to prompt the user for (an integer)
blood-sugar reading (BSR) and print a warning message if it is over
500. Also, print the message “>> Invalid input” if the user
enters a non-integer, such as a float or a string.
Having completed Worked_Exercise_5.1 above you should use that code
as the basis for this problem.
Use this sample interaction to demonstrate your code:
Enter a BSR: 155
Enter a BSR: string input
>> Invalid input
Enter a BSR: 522
** Warning! Blood sugar reading over 500: 522
Enter a BSR: 485
Enter a BSR: 578
** Warning! Blood sugar reading over 500: 578
Enter a BSR: 210
Enter a BSR: 330.6
>> Invalid input
Enter a BSR: 519
** Warning! Blood sugar reading over 500: 519
Part (b)
Create a list of BSRs as follows:
blood_sugar_readings = [155, 190, 522, 485, 578, 210, 130,
519]
Use a for-loop to iterate through the list in order to find the
highest and lowest BSR values. Note that finding the maximum and
minimum values of a list using a for-loop is shown in Section 5.7.2
of the textbook on p. 62-3. (Note that since we are ‘hard-coding’
the list, we may assume the data is valid, i.e., it has been
previously checked.)
HINT: as is done in the textbook, while developing the code, add a
print statement as the last line of the for-loop writing out the
current BSR value, the smallest, and the largest. Then when your
code is running correctly, you can comment out that line (but leave
it in the code-base).
After looping through the list, print the highest and lowest BSR
values.
What to hand in:
Take screen snips of the Run window for Parts (a) and (b). Use the
snipping tool on the console portion of the screen – but including
the header bar and the file name.
Copy the snips into a Word document (filename
Hwk06_YourLastName_ScreenSnips.doc) with your name and date in the
upper right hand corner.
Upload the Word doc and the Python program files
Hwk06a_YourLastName.py and Hwk06b_YourLastName.py to the
appropriate assignment in the LMS. Three files total.
NOTE: As was described in the previous assignment, each Python file
you create should be documented with (minimally) the following
three lines at the top of each file:
# Filename: Hwk06_YourLastName.py
# Author: <Your Name>
# Date Created: 2017/09/05
# Purpose: <brief description>
def checkBSR(val):
if(val > 500):
print("** Warning! Blood sugar reading over 500:{}".format(val))
def checkBSRList(BSRlist):
minNumber = 9999
maxNumber = -1
for i in BSRlist:
minNumber = min(minNumber,i)
maxNumber = max(maxNumber,i)
print("max BSR value is {}".format(maxNumber)+" min BSR value is {}".format(minNumber))
if __name__ == "__main__":
try:
BSR = int(input('Enter BSR:'))
checkBSR(BSR)
except:
print(">> Invalid input")
blood_sugar_readings = [155, 190, 522, 485, 578, 210, 130, 519]
checkBSRList(blood_sugar_readings)
Get Answers For Free
Most questions answered within 1 hours.