Choosing a specific book
choosing_a_book.py
import sqlite3
db = sqlite3.connect('bookshop.db')
result = db.cursor().execute("SELECT * FROM books").fetchall()
db.close()
n = len(result)
i = 0
while i < n:
print(i, result[i][1]) #e.g.: 10 The Gruffalo
i = i + 1
choice = int(input("Enter book num >> "))
print("Your book choice: ", result[choice])
- Run the above code file
- Instead of printing the whole tuple of the book I have chosen (which is what the last line of code does), see if you can make the code print just the cost of the book I have chosen. Hint:
result[choice][?], where?is the integer index of the field that contains the price.. - create a variable
walletthat contains the integer value 50 (representating $50 of cash in my wallet). When i choose a book to purchase, subtract the chosen book price field from the wallet variable. For example:10 The Gruffalo 11 Jamie's 30-Minute Meals 12 The Girl Who Kicked the Hornets' Nest Enter book num >> 10 Your book price: 21.95 Wallet remaining: 28.05
Core Activities:
Extension Activities: