PYTHON PROJECTS FOR BEGINNERS


In this post, you see a beginner Python program, which is a simple shopping cart program. Let us learn with today's post.
BOOK = []
PRICE = []
TOTAL = 0
while True:
    BOOKS = input("ENTER THE BOOK NAME OR q TO QUIT:")
    if BOOKS.lower() == "q":
        break
    else:
        PRICES = float(input(f"ENTER A PRICE OF BOOK A {BOOKS}:"))
        BOOK.append(BOOKS)
        PRICE.append(PRICES)
print("SHOPPING LIST")
print("ITEMS")
for BOOKS in BOOK:
    print(BOOKS)
for PRICES in PRICE:
    TOTAL += PRICES
print(f"THE TOTAL PRICE IS {TOTAL}")
Let us start with a step-by-step explanation of the above code.
Step 1: BOOK = []

Here, BOOK is a variable declared to store the user's input of book names, which are stored in Lists [].

Step 2: PRICE = []

Like Step 1, here also, PRICE is a variable declared to save the price of the book that the user inputs in Lists [].

Step 3: TOTAL = 0

Here, the final TOTAL amount is calculated as an integer, so it is declared as an integer equal to 0 in the code.
So, we declared all the variables that are needed for the simple shopping cart program.

Step 4: while True:

Now, while is used here because it creates an infinite loop, it means the code block indented below the while True: statement will execute repeatedly without any inherent stopping condition based on the while statement itself. So while is used here in the program.

Step 5: BOOKS = input("ENTER THE BOOK NAME OR q TO QUIT:")

Now we are asking input from the users to enter the BOOKS name, which is stored in the BOOKS variable, or "q" to quit the program.

Step 6: if BOOKS.lower() == "q":

It means if the user enters the Capital "Q", it converts into a lower letter by using the [.lower()] function in a Python program. Then == In Python Means, == is the equality operator, used to compare the values of two operands in the program. It returns True if the values of the operands are equal, otherwise False.

Step 7: 
             break
      else:

The break statement terminates the loop containing it in the program.
The else: statement is primarily used in conjunction with conditional statements.

Step 8:

PRICES = float(input(f"ENTER A PRICE OF BOOK A {BOOKS}:"))

Now we are asking input from the users to enter the PRICES of the BOOKS, which are stored in the PRICES variable.

Step 9: 
BOOK.append(BOOKS)
              PRICE.append(PRICES)


The append() method in Python is a built-in list method used to add a single element to the end of an existing list of a variable declared in the program.
So, we are using the append() function here to add the input of users' BOOKS and PRICE in the Variable declared in Steps 1 and 2.

Step 10: print("SHOPPING LIST")
                print("ITEMS")
Here is some print() function is used to print the name of the SHOPPING LIST and ITEMS.

Step 11:

for BOOKS in BOOK:
    print(BOOKS)
for PRICES in PRICE:
TOTAL += PRICES

for BOOKS in BOOK: → Look at each book in the BOOK list.
print(BOOKS) → Show the name of that book on the screen.
for PRICES in PRICE: → Look at each price in the PRICE list.
TOTAL += PRICES → Add each price to the total amount.

Step 12: 
print(f"THE TOTAL PRICE IS {TOTAL}")

The final line of the program prints the total amount of the shopping cart in the Output.
Sample Output:
ENTER THE BOOK NAME OR q TO QUIT:Math ENTER A PRICE OF BOOK A Math:100 ENTER THE BOOK NAME OR q TO QUIT:Science ENTER A PRICE OF BOOK A Science:200 ENTER THE BOOK NAME OR q TO QUIT:English ENTER A PRICE OF BOOK A English:150 ENTER THE BOOK NAME OR q TO QUIT:q SHOPPING LIST ITEMS Math Science English THE TOTAL PRICE IS 450.0
Here is a Simple beginner-friendly Python program to practice yourself when learning the program.