Python Forum
A CLI based shopping program written in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A CLI based shopping program written in Python
#1
#MINI SHOPPING CLI PROGRAM 
import time
import random

item = ["Laptop","T-Shirt","Pants"] #You may add your own items
cost = ["Rs 35600","Rs 2000","Rs 1200"] #You may add your own price of your items but make sure you update the program necessarily. 
x = []
l = []
print()
for i,j in zip(item,cost):
    print(i,"=",j)
    print()
choice = 'y' or 'n'
count = 0

while choice == 'y' or choice == 'YES' or choice == 'yes' or choice =='Y':
    print()
    user_1 = input("Enter the name of the item which you would like to purchase : ")
    print()    
    user_2 = int(input("Enter the cost of the item which you would like to purchase : {} ".format("Rs")))
    print()
    if user_2 == 35600 or user_2 == 2000 or user_2 == 1200:
        l.append(user_2)
    else:
        print()
        print("DONT CHEAT. ABOTING IN 5 SECONDS.")
        time.sleep(5)
        break
    if user_1 in item:
        x.append(user_1)
        print()
        print(user_1,"has been added to your cart.")
        print()
        count += 1
    else:
        print()
        print("Item not found.Try restarting the app.")
    choice = input("Do you want to add any other item to your cart : ")

while choice == 'n' or choice == 'no' or choice == 'NO' or choice == 'N':
    print()
    print("There are",count,"items in your cart")
    print()
    print("Total : {}".format("Rs"),sum(l))
    print()
    user_4 = input("Proceed to checkout (y/n) : ")
    if user_4 == 'n':
        print()
        print("ABORTING IN 5 SECONDS")
        time.sleep(5)
        break
    else:
        print()
        user_5 = input("Select payment method (Credit/Debit) : ")
        print()
        print("PROCESSING")
        r = 0
        while r <= 2:
            print(".")
            time.sleep(1)
            r += 1
        print()
        print("GENERATING CAPTCHA")
        b = 0
        while b <= 3:
            print(".")
            time.sleep(1)
            b += 1
        print()
        print("Enter the captcha given below.")
        print()
        captcha = random.randint(111111,999999)
        print(captcha)
        print()
        user_6 = input()
        if user_6 != captcha:
            "ABORTING IN 5 SECONDS."
        else:
            continue
        f = 0
        print()
        print("AWAITING IMFORMATION.")
        while f <= 5:
            print(".")
            time.sleep(1)
            f += 1
        print()
        print("TRANSACTION SUCCESSFUL.")
        print()
        print("Thank You for choosing Muhammed©")
        time.sleep(15)
        break
Feel free to update this program if you think this program could be coded even better **dance**
Please don't forget to leave a like.
Reply
#2
Not bad. A few tips/pointers:

I would put the items in a dictionary. Have the items be keys and the costs be values (as integers). I would think the system would tell you the cost of the thing you purchased. Currently your system allows buying a laptop for 1200.

I don't understand the format on the user_2 input. Why format a string literal with another string literal? Unless you're thinking of expanding to other currencies, put Rs in the first string. Also, I would make the names user_1 and user_2 more descriptive, like user_item and user_cost. It makes your code easier to read.

The in operator and the lower method of strings would greatly simplify your conditionals. Take this line:

while choice == 'n' or choice == 'no' or choice == 'NO' or choice == 'N':
That could be rewritten as:

while choice.lower() in ('n', 'no'):
Now you have all four possibilities taken care of with one operator.

Keep at it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Oct-22-2018, 01:43 PM)ichabod801 Wrote: Not bad. A few tips/pointers:

I would put the items in a dictionary. Have the items be keys and the costs be values (as integers). I would think the system would tell you the cost of the thing you purchased. Currently your system allows buying a laptop for 1200.

I don't understand the format on the user_2 input. Why format a string literal with another string literal? Unless you're thinking of expanding to other currencies, put Rs in the first string. Also, I would make the names user_1 and user_2 more descriptive, like user_item and user_cost. It makes your code easier to read.

The in operator and the lower method of strings would greatly simplify your conditionals. Take this line:

while choice == 'n' or choice == 'no' or choice == 'NO' or choice == 'N':
That could be rewritten as:

while choice.lower() in ('n', 'no'):
Now you have all four possibilities taken care of with one operator.

Keep at it.

Thank you so much for correcting my program. It was a foolish move for me to use list instead of dictionary, so here is my updated code. Please reply back if there is any error. :)

#MINI SHOPPING CLI PROGRAM 
import json
import time
import random

dic = dict([["Laptop",35600],["T-Shirt",2000],["Pants",1200]])
print(json.dumps(dic,indent = 6))
choice = 'y' or 'n'
count = 0
l =[]
x =[]
while choice.lower() in ('y','yes'):
    print()
    user_1 = input("Enter the name of the item which you would like to purchase : ")
    print()    
    user_2 = int(input("Enter the cost of the item which you would like to purchase : {} ".format("Rs")))
    print()
    if user_2 == dic[user_1]:
        l.append(user_2)
    else:
        print()
        print(user_1,"has a price of",dic[user_1],"and not",user_2)
        time.sleep(5)
        break
    if user_1 in dic:
        x.append(user_1)
        print()
        print(user_1,"has been added to your cart.")
        print()
        count += 1
    else:
        print()
        print("Item not found.Try restarting the app.")
    choice = input("Do you want to add any other item to your cart : ")

while choice.lower() in ('n','no'):
    print()
    print("There are",count,"items in your cart")
    print()
    print("Total : {}".format("Rs"),sum(l))
    print()
    user_4 = input("Proceed to checkout (y/n) : ")
    if user_4 == 'n':
        print()
        print("ABORTING IN 5 SECONDS")
        time.sleep(5)
        break
    else:
        print()
        user_5 = input("Select payment method (Credit/Debit) : ")
        print()
        print("PROCESSING")
        r = 0
        while r <= 2:
            print(".")
            time.sleep(1)
            r += 1
        print()
        print("GENERATING CAPTCHA")
        b = 0
        while b <= 3:
            print(".")
            time.sleep(1)
            b += 1
        print()
        print("Enter the captcha given below.")
        print()
        captcha = random.randint(111111,999999)
        print(captcha)
        print()
        user_6 = input()
        if user_6 != captcha:
            "ABORTING IN 5 SECONDS."
        else:
            continue
        f = 0
        print()
        print("AWAITING IMFORMATION.")
        while f <= 5:
            print(".")
            time.sleep(1)
            f += 1
        print()
        print("TRANSACTION SUCCESSFUL.")
        print()
        print("Thank You for choosing Muhammed©")
        time.sleep(15)
        break
Reply
#4
Guys please rate this thread i would appreciate it very much if you did. Thank You
Reply
#5
We don't tend to rate threads around here. Look around, almost none of the threads are rated.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(Oct-22-2018, 04:31 PM)Ablazesphere Wrote: Guys please like my post i would appreciate it very much if you did. Thank You
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020