Question

In: Computer Science

HIMT 345 Homework 06: Iteration Overview: Examine PyCharm’s “Introduction to Python” samples for Loops. Use PyCharm...

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>

Solutions

Expert Solution

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


Related Solutions

HIMT 345 Homework 05: Functions Overview: Examine PyCharm’s “Introduction to Python” samples for Functions. Use PyCharm...
HIMT 345 Homework 05: Functions Overview: Examine PyCharm’s “Introduction to Python” samples for Functions. Use PyCharm to work along with exercises from Chapter 4. Modify the grade assignment program of Hwk 04 to utilize a function by copying the Hwk04 project and creating the Hwk05 project. Prior Task Completion: 1. Read Chapter 04 2. View Chapter 04’s video notes. 3. As you view the video, work along with each code sample in PyCharm. The examples used in the video are...
HIMT 345 Homework 05: Functions Overview: Examine PyCharm’s “Introduction to Python” samples for Functions. Use PyCharm...
HIMT 345 Homework 05: Functions Overview: Examine PyCharm’s “Introduction to Python” samples for Functions. Use PyCharm to work along with exercises from Chapter 4. Modify the grade assignment program of Hwk 04 to utilize a function by copying the Hwk04 project and creating the Hwk05 project. Prior Task Completion: 1. Read Chapter 04 2. View Chapter 04’s video notes. 3. As you view the video, work along with each code sample in PyCharm. The examples used in the video are...
Python code Assignment Overview In this assignment you will practice with conditionals and loops (for). This...
Python code Assignment Overview In this assignment you will practice with conditionals and loops (for). This assignment will give you experience on the use of the while loop and the for loop. You will use both selection (if) and repetition (while, for) in this assignment. Write a program that calculates the balance of a savings account at the end of a period of time. It should ask the user for the annual interest rate, the starting balance, and the number...
Rewrite your program for finding Pascal's Triangle to use iteration (loops) instead of recursion. Include in...
Rewrite your program for finding Pascal's Triangle to use iteration (loops) instead of recursion. Include in both algorithms code to keep track of the total time used to run the algorithms in milliseconds. Run these algorithms for n = 10, n = 20, and n = 30. Print out both the original output and the time to run for both algorithms. Please make sure you comment your code thoroughly. The code should be nicely formatted and should use proper variables.
1. what is the best wavelength to use to examine the smallest visible samples? Why? 2....
1. what is the best wavelength to use to examine the smallest visible samples? Why? 2. If the resolution of an FTIR depends on the distance the mirror travels, why are relatively long moving distances such as 2 or 4 cm not used? What is the trade-off? 3. Which of the three contributing factors to band broadening in chromatography is independend of the flow rate? Explain why.
Homework #3-2 a) (Use software for this problem) Four independent samples are collected form four normally...
Homework #3-2 a) (Use software for this problem) Four independent samples are collected form four normally distributed populations. The data are:         Group 1:       12     11     14     10     12     10         Group 2:       14     12     16     15         Group 3:       17     18     20     22     23         Group 4:       10     9       13     13 The SST is equal to 305.68. Conduct a test...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT