This tutorial gives you the solutions to the exercises in Chapter 3 of the book Python for Everybody: Exploring Data Using Python 3 by Charles R. Severance.
Link to the book:
Solutions for other chapters:
- [Solved] Python for Everybody - Chapter 2: Variables, Expressions, and Statements
- [Solved] Python for Everybody - Chapter 3: Conditional Execution (this tutorial)
- [Solved] Python for Everybody - Chapter 5: Iteration
- [Solved] Python for Everybody - Chapter 6: Strings
- [Solved] Python for Everybody - Chapter 8: Lists
Contents
- 1. Chapter 3: Conditional Execution - Exercise 1
- 2. Chapter 3: Conditional Execution - Exercise 2
- 3. Chapter 3: Conditional Execution - Exercise 3
1. Chapter 3: Conditional Execution - Exercise 1
Exercise 1: Rewrite your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours.
Answer:
hours = float(input('Enter Hours: ')) rate = float(input('Enter Rate: ')) pay = hours * rate if hours > 40: pay += (hours - 40) * rate * 0.5 print('Pay:', pay)
Output:
python exercise-1.py Enter Hours: 45 Enter Rate: 10 Pay: 475.0
Explanation:
- Get the number of hours worked and the hourly rate from the user using the
input()
function. - Calculate the pay by multiplying the number of hours worked with the hourly rate.
- If the number of hours worked is greater than 40, add the extra pay to the total pay, which is calculated by multiplying the number of extra hours worked with the hourly rate and the overtime rate of 1.5.
- Print the total pay.
2. Chapter 3: Conditional Execution - Exercise 2
Exercise 2: Rewrite your pay program using try
and except
so that your program handles non-numeric input gracefully by printing a message and exiting the program. The following shows two executions of the program:
Enter Hours: 20 Enter Rate: nine Error, please enter numeric input Enter Hours: forty Error, please enter numeric input
Answer:
try: hours = float(input('Enter Hours: ')) rate = float(input('Enter Rate: ')) except: print('Error, please enter numeric input') exit() pay = hours * rate if hours > 40: pay += (hours - 40) * rate * 0.5 print('Pay:', pay)
Output:
python exercise-2.py Enter Hours: 20 Enter Rate: nine Error, please enter numeric input
python exercise-2.py Enter Hours: forty Error, please enter numeric input
Explanation:
- Use the
try
andexcept
statements to handle non-numeric input. Theexcept
block will be executed if thetry
block raises an error, which is caused by using thefloat()
function to convert non-numeric input to a float. - If the user enters non-numeric input, print an error message and exit the program.
- Otherwise, calculate the pay similarly to the previous exercise.
- Print the total pay.
3. Chapter 3: Conditional Execution - Exercise 3
Exercise 3: Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error message. If the score is between 0.0 and 1.0, print a grade using the following table:
Score | Grade |
---|---|
>= 0.9 | A |
>= 0.8 | B |
>= 0.7 | C |
>= 0.6 | D |
< 0.6 | F |
Answer:
try: score = float(input('Enter score: ')) except: print('Bad score') exit() if score < 0.0 or score > 1.0: print('Bad score') elif score >= 0.9: print('A') elif score >= 0.8: print('B') elif score >= 0.7: print('C') elif score >= 0.6: print('D') else: print('F')
Output:
python exercise-3.py Enter score: 0.95 A
python exercise-3.py Enter score: perfect Bad score
python exercise-3.py Enter score: 10.0 Bad score
python exercise-3.py Enter score: 0.75 C
python exercise-3.py Enter score: 0.5 F
Explanation:
- Similar to the previous exercise, use the
try
andexcept
statements to handle non-numeric input. - If the user enters non-numeric input, print
Bad score
and exit the program. - Otherwise, check if the score is out of range. If so, print
Bad score
and exit the program. - Otherwise, check the score against each grade and print the corresponding grade.
Comments
Be the first to comment!