Exercise 11-04:

Modify the Exercise 11.3 program to ask for two values and list all the products with prices between those two values.

Answer:

##############################################################################
# Python From Scratch
# Author: Nilo Ney Coutinho Menezes
# Editora Novatec (c) 2010-2025 - LogiKraft 2025
# Site: https://pythonfromscratch.com
# ISBN: 978-85-7522-949-1 (Paperback), 978-85-7522-950-7 (hardcover), 978-85-7522-951-4 (ebook)
#
# File: chapter 11/exercise-11-04.py.py
##############################################################################
import sqlite3
from contextlib import closing

with sqlite3.connect("prices.db") as connection:
    with closing(connection.cursor()) as cursor:
        price1 = input("Enter the minimum price to list: ")
        price2 = input("Enter the maximum price to list: ")

        cursor.execute(
            """select * from prices
                          where price >= ? and price <= ?""",
            (price1, price2),
        )
        found = 0
        for result in cursor.fetchall():
            print("Name: {0:30s} Price: {1:6.2f}".format(*result))
            found += 1
        if found == 0:
            print("Not found.")
        else:
            print("{} product(s) found.".format(found))
Click here to download the file