fork download
  1. CREATE DATABASE shopping_db;
  2.  
  3. USE shopping_db;
  4.  
  5. CREATE TABLE items (
  6. id INT AUTO_INCREMENT PRIMARY KEY,
  7. name VARCHAR(50),
  8. price INT
  9. );
  10.  
  11. INSERT INTO items (name, price) VALUES
  12. ('Rice', 160),
  13. ('Potato', 80),
  14. ('Tomato', 70),
  15. ('Wheat', 150),
  16. ('Pulses', 180),
  17. ('Onion', 100),
  18. ('Sugar', 80),
  19. ('Salt', 60),
  20. ('Spices', 100);
  21. import mysql.connector
  22.  
  23. # Establish a connection to the database
  24. db = mysql.connector.connect(
  25. host="localhost", # Change to your MySQL host
  26. user="root", # MySQL username
  27. password="yourpassword", # MySQL password
  28. database="shopping_db" # Database name
  29. )
  30.  
  31. cursor = db.cursor()
  32.  
  33. # Function to display available items
  34. def display_items():
  35. cursor.execute("SELECT id, name, price FROM items")
  36. items = cursor.fetchall()
  37. print("\nAvailable items:")
  38. for item in items:
  39. print(f"{item[0]}. {item[1]} - Rs. {item[2]}")
  40.  
  41. # Function to handle item selection
  42. def select_items():
  43. selected_items = []
  44. while True:
  45. try:
  46. item_id = int(input("\nEnter the item ID to select (or 0 to finish): "))
  47. if item_id == 0:
  48. break
  49. cursor.execute("SELECT name, price FROM items WHERE id = %s", (item_id,))
  50. item = cursor.fetchone()
  51. if item:
  52. selected_items.append(item)
  53. print(f"Added {item[0]} to the cart.")
  54. else:
  55. print("Invalid item ID. Please try again.")
  56. except ValueError:
  57. print("Please enter a valid number.")
  58.  
  59. return selected_items
  60.  
  61. # Function to calculate total price
  62. def calculate_bill(selected_items):
  63. total_price = sum(item[1] for item in selected_items)
  64. return total_price
  65.  
  66. # Function to print the bill
  67. def print_bill(selected_items, total_price):
  68. print("\nFinal Bill:")
  69. print("------------")
  70. for item in selected_items:
  71. print(f"{item[0]} - Rs. {item[1]}")
  72. print(f"Total: Rs. {total_price}")
  73.  
  74. def main():
  75. display_items() # Show available items
  76. selected_items = select_items() # Allow user to select items
  77. if selected_items:
  78. total_price = calculate_bill(selected_items) # Calculate total price
  79. print_bill(selected_items, total_price) # Show final bill
  80. else:
  81. print("No items selected.")
  82.  
  83. # Close the database connection
  84. db.close()
  85.  
  86. # Run the program
  87. if _name_ == "_main_":
  88. main()
  89.  
Success #stdin #stdout 0.02s 26040KB
stdin
Standard input is empty
stdout
CREATE DATABASE shopping_db;

USE shopping_db;

CREATE TABLE items (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50),
    price INT
);

INSERT INTO items (name, price) VALUES 
('Rice', 160),
('Potato', 80),
('Tomato', 70),
('Wheat', 150),
('Pulses', 180),
('Onion', 100),
('Sugar', 80),
('Salt', 60),
('Spices', 100);
import mysql.connector

# Establish a connection to the database
db = mysql.connector.connect(
    host="localhost",  # Change to your MySQL host
    user="root",  # MySQL username
    password="yourpassword",  # MySQL password
    database="shopping_db"  # Database name
)

cursor = db.cursor()

# Function to display available items
def display_items():
    cursor.execute("SELECT id, name, price FROM items")
    items = cursor.fetchall()
    print("\nAvailable items:")
    for item in items:
        print(f"{item[0]}. {item[1]} - Rs. {item[2]}")

# Function to handle item selection
def select_items():
    selected_items = []
    while True:
        try:
            item_id = int(input("\nEnter the item ID to select (or 0 to finish): "))
            if item_id == 0:
                break
            cursor.execute("SELECT name, price FROM items WHERE id = %s", (item_id,))
            item = cursor.fetchone()
            if item:
                selected_items.append(item)
                print(f"Added {item[0]} to the cart.")
            else:
                print("Invalid item ID. Please try again.")
        except ValueError:
            print("Please enter a valid number.")

    return selected_items

# Function to calculate total price
def calculate_bill(selected_items):
    total_price = sum(item[1] for item in selected_items)
    return total_price

# Function to print the bill
def print_bill(selected_items, total_price):
    print("\nFinal Bill:")
    print("------------")
    for item in selected_items:
        print(f"{item[0]} - Rs. {item[1]}")
    print(f"Total: Rs. {total_price}")

def main():
    display_items()  # Show available items
    selected_items = select_items()  # Allow user to select items
    if selected_items:
        total_price = calculate_bill(selected_items)  # Calculate total price
        print_bill(selected_items, total_price)  # Show final bill
    else:
        print("No items selected.")

    # Close the database connection
    db.close()

# Run the program
if _name_ == "_main_":
    main()