[Solved] Python for Everybody - Chapter 3: Conditional Execution

Minh Vu

By Minh Vu

Updated Jan 05, 2024

Figure: [Solved] Python for Everybody - Chapter 3: Conditional Execution

Disclaimer: All content on this website is derived directly from my own expertise and experiences. No AI-generated text or automated content creation tools are used.

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:

Contents

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:

exercise-1.py
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:

console
python exercise-1.py Enter Hours: 45 Enter Rate: 10 Pay: 475.0

Explanation:

  1. Get the number of hours worked and the hourly rate from the user using the input() function.
  2. Calculate the pay by multiplying the number of hours worked with the hourly rate.
  3. 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.
  4. 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:

exercise-2.py
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:

console
python exercise-2.py Enter Hours: 20 Enter Rate: nine Error, please enter numeric input
console
python exercise-2.py Enter Hours: forty Error, please enter numeric input

Explanation:

  1. Use the try and except statements to handle non-numeric input. The except block will be executed if the try block raises an error, which is caused by using the float() function to convert non-numeric input to a float.
  2. If the user enters non-numeric input, print an error message and exit the program.
  3. Otherwise, calculate the pay similarly to the previous exercise.
  4. 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:

ScoreGrade
>= 0.9A
>= 0.8B
>= 0.7C
>= 0.6D
< 0.6F

Answer:

exercise-3.py
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:

console
python exercise-3.py Enter score: 0.95 A
console
python exercise-3.py Enter score: perfect Bad score
console
python exercise-3.py Enter score: 10.0 Bad score
console
python exercise-3.py Enter score: 0.75 C
console
python exercise-3.py Enter score: 0.5 F

Explanation:

  1. Similar to the previous exercise, use the try and except statements to handle non-numeric input.
  2. If the user enters non-numeric input, print Bad score and exit the program.
  3. Otherwise, check if the score is out of range. If so, print Bad score and exit the program.
  4. Otherwise, check the score against each grade and print the corresponding grade.
Minh Vu

Minh Vu

Software Engineer

Hi guys 👋, I'm a developer specializing in Elastic Stack and Next.js. My blog shares practical tutorials and insights based on 3+ years of hands-on experience. Open to freelance opportunities — let's get in touch!

Comments

Be the first to comment!

Leave a Comment

Receive Latest Updates 📬

Get every new post, special offers, and more via email. No fee required.