External Exam Download Resources Web Applications Games Recycle Bin

Console menu interface for Bookshop

console_menu.py

import sqlite3
db = sqlite3.connect('bookshop.db')
result = db.cursor().execute("SELECT * FROM books").fetchall()
db.close()

menu = """Bookshop menu.
1: Choose book
2: Exit
>> """

menu_choice = int(input(menu))

while menu_choice != 2:
    if menu_choice == 1:
        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])

    menu_choice = int(input(menu))

    Core Activities:

  1. Clean up the code as you did in the previous set of activities, so that this doesn't print the whole tuple of the book I choose, but rather the title or price.
  2. Add a goodbye message to the above code, so that when I choose exit, the program says "Thankyou, come again." as a final print.
  3. Extension Activities:

  4. Add in the wallet system (start with $50) that you implemented in the previous activity
  5. Prevent me from buying more books than I can afford. If I choose to purchase a book that I can't afford, please give me an appropriate error message.
  6. Your program should tell me how short of funds I am for the book I want. For example, if i have $10 left in my wallet, and a book costs $12, the program should say "You are $2 short".