Question
After reviewing this week's learning resources, complete the following for your coding practice lab activity. Textbook Exercises Complete the Exercises in the textbook before
After reviewing this week's learning resources, complete the following for your coding practice lab activity.
Textbook Exercises
Complete the Exercises in the textbook before moving onto the next assignment.
Exercise 3-1: Enhance the Miles Per Gallon program
Using this Starter Code:
Links to an external site.
print("The Miles Per Gallon program\n")
# Create loop to get user input for multiple trips
while True:
# Get miles driven from the user
miles_driven = float(input("Enter miles driven: "))
# Get gallons of gas used from the user
gallons_used = float(input("Enter gallons of gas used: "))
# Get cost per gallon from the user
cost_per_gallon = float(input("Enter cost per gallon: "))
# Calculate the miles per gallon
#-------Your Code Here----------
# Calculate the total gas cost
#-------Your Code Here----------
# Calculate the cost per mile
#-------Your Code Here----------
# Print the result
#-------Your Code Here----------
# Ask the user if they want to enter another trip
another_trip = input("\nGet entries for another trip (y/n)? ")
if another_trip.lower() != "y":
break
enhance the Miles Per Gallon program so it lets the user repeat the entries and get the miles per gallon for more than one trip. To do that, use a while loop.
Alternately, if you are using your local computer for development, open the test_scores.py file in the following folder: murach_python > exercises > ch03 > mpg.py from the downloaded textbook files.
For an example of a y/n while loop see main.pyDownload main.py(Links to an external site.)
Modify the program so it gets the cost of a gallon of gas as another entry from the user, and validate this entry before using it in your calculations. If all three entries are valid, calculate the total gas cost and the cost per mile, and display the results on the screen.
Example output: (Bold fonts are the user input fields)
The Miles Per Gallon program
Enter miles driven: 150
Enter gallons of gas used: 15.2
Enter cost per gallon: 4.25
Miles Per Gallon: 9.87
Total Gas Cost: 64.6
Cost Per Mile 0.4
Get entries for another trip (y/n)? y
Enter miles driven: 225
Enter gallons of gas used: 16
Enter cost per gallon: 4.25
Miles Per Gallon: 14.06
Total Gas Cost: 68.0
Cost Per Mile 0.3
Get entries for another trip (y/n)? n
Exercise 3-2: Enhance the Test Scores program
Using this starter Code:
print("The Test Scores program\n")
print("Enter test scores")
print("Enter 'end' to end input")
print("===============================")
# Outer loop to manage multiple sets of scores
while True:
total_score = 0
count = 0
# Inner loop to input scores for a single set
while True:
score_input = input("Enter test score: ")
# Check if input is "end"
if score_input.lower() == "end":
break
# Convert input to integer for processing
# ---- Your code here ----------
# Validate the input score
if 0 <= score <= 100:
# ---- Your code here ----------
else:
# ---- Your code here ----------
# Calculate the average score
# ---- Your code here ----------
# Ask the user if they want to enter another set of scores
another_set = input("Enter another set of test scores (y/n)? ")
if another_set.lower() != "y":
break
else:
print("\nEnter test scores")
print("Enter 'end' to end input")
print("===============================")
enhance the Test Scores program so it lets the user input multiple sets of scores. To do that, use a nested while loop.
Alternately, if you are using your local computer for development, open the test_scores.py file in the following folder: murach_python > exercises > ch03 > test_scores.py from the downloaded textbook files.
Modify the program so that when the user enters "end" it ends the set of score entries and outputs the Total and Average Score for the set that was just finished. Then ask if there is another set of test scores to enter. Be sure to keep the validation for the number input to be between 0 and 100.
Hint for Exercise 3-2
- recall that by default input() comes over as a string variable
- check FIRST if the input() equals END with an IF statement
- ELSE change the variable to an int() and evaluate from there
Example output: (Bold fonts are the user input fields)
The Test Scores program
Enter test scores
Enter 'end' to end input
===============================
Enter test score: 75
Enter test score: 85
Enter test score: 95
Enter test score: end
===============================
Total Score: 255
Average Score: 85Enter another set of test scores (y/n)? y
Enter test scores
Enter 'end' to end input
===============================
Enter test score: 95
Enter test score: -85
Test score must be from 0 through 100. Try again.
Enter test score: 85
Enter test score: 60
Enter test score: end
===============================
Total Score: 240
Average Score: 80
Enter another set of test scores (y/n)? n
To Submit Your Programs:
Submit the .py files when you are done.
Rubric
Lab Activity 3
Criteria | Ratings | Pts | |||||
---|---|---|---|---|---|---|---|
This criterion is linked to a Learning OutcomeProper Code Syntax, Format and Supported with Comments
|
| 2 pts | |||||
This criterion is linked to a Learning OutcomeCreated working program 1 meeting the assignment instructions, free from errors
|
| 4 pts | |||||
This criterion is linked to a Learning OutcomeCreated working program 2 meeting the assignment instructions, free from errors
|
| 4 pts | |||||
Total Points: 10 |
PreviousNext
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started