In: Computer Science
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_”. Create two new python
files within that project called “Hwk06a_” and “Hwk06b_”.
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 descriptions>
Part a:
# Defines a function to validate the bsr
# Returns true if parameter bsr is integer
# Displays erro message and returns false if it is float or
string
def validInput(bsr):
# Initially empty
value = None
# try block begins
try:
# Converts the parameter bsr to integer
value = int(bsr)
# Handles the exception
except ValueError:
# try block begins
try:
# Converts the parameter bsr to float
value = float(bsr)
# Handles the exception for string parameter
except ValueError:
print(">> Invalid input")
return False
# Otherwise float parameter
else:
print(">> Invalid input")
return False
# Returns true for integer
return True
# Loops till valid input
while(True):
# Accepts data
bsr = input("Enter a BSR: ")
# Calls the function to validate data
# If the return result is True
if validInput(bsr) == True:
# Converts the entered data to integer
bsr = int(bsr)
# Checks if entered data is greater than 500
# displays error message
if bsr > 500:
print("** Warning! Blood sugar reading over 500: ", bsr)
# Otherwise valid data stop the loop
else:
break
Sample Output:
Enter a BSR: Twenty
>> Invalid input
Enter a BSR: 23.6
>> Invalid input
Enter a BSR: 20
---------------------------------------------------------------------------------------------------------------
Part b:
# Creates a list with blood sugar
blood_sugar_readings = [155, 190, 522, 485, 578, 210, 130, 519]
# Stores the first value as the highest
high = blood_sugar_readings[0]
# Stores the first value as the lowest
low = blood_sugar_readings[0]
# Loops from 1 to end of the list (because 0 is considered
above)
for index in range(1, len(blood_sugar_readings)):
# Checks if current index value is greater then earlier high
if blood_sugar_readings[index] > high:
# Assigns the current index value as highest
high = blood_sugar_readings[index]
# Checks if current index value is less then earlier low
if blood_sugar_readings[index] < low:
# Assigns the current index value as lowest
low = blood_sugar_readings[index]
# Displays the highest and lowest
print("Highest BSR value: ", high)
print("Lowest BSR value: ", low)
Sample Output:
Highest BSR value: 578
Lowest BSR value: 130