Hello
i need to make a cash register but i dont know how to do it.
i was thinking about to make it in loop but i dont know how to make its. i need to input a barcode of product each barcode has price an stock level. In the loop i will input a barcode all the time untile i input "Done" and then it will out put me total price and show me how mach left in the stok.

abd i was thinking do make it in dicturiany but i dont know how to make the program take out the price.

Thank for any help ;D
in Python 2.7.3

A few questions:
* Is this a Point of Sale system for an actual shop, or a school project?
* When you say that you need to input a barcode, do you mean you would type in an SKU number by hand, or do you actually need to scan barcodes? If it really is reading a scanner, what kind of scanner, and what operating system are you using? Do you have a driver for the scanner which comes with an accessible API?
* Do you intend for this to be a GUI application, or a console application? If the former, are you going to use TkInter (the one that comes with Python, which is good enough for something like this but not necessarily the best) or a 3rd party toolkit such as PyQt4 or wxPython?
* When you say that you don't know how to make the loop, do you mean you aren't familiar with the syntax of while: loops, or that you aren't sure how to determine the end condition?
* When you say you were looking to use a Dictionary, do you mean for the inventory items?
* For a practical system, a database of some sort is pretty much a necessity. Do you know what sort of database you intend to use, and how to connect to it using Python?

This is a school project not for store ;D . Each product has price name and barcode and stock level.
i will ned to INPUT a barcode. I dont need some think sireiuse i was thinking to make it in loop so each time in the loop it will ask for barckode i will input him again and again until i will type DONE and the i will out put me the total price and how mach left in stock.

Thank for help ;P

Hmmn, OK. Let's start with the data structure for the items. Has your instructor covered classes yet? If so, I would recommend a class which would have the name, price and stock count as the instance variables, and the have a dictionary using the barcode as the key and the Item objects as the value.

If you haven't covered classes yet, you can still do the same sort of thing with nested dictionaries. You can have each item represented by a dictionary, and then use that dictionary as the value for another dictionary with the barcode as it's key. You'd have one inner dictionary for each item. You can then wrap that up in a functions to create the dictionaries for you, and handle setting and getting the values:

def makeitem(name, price, stock_level):
    return {"name": name, "price": price, "stock level": stock_level};

def subtractFromStock(inventory, barcode, amount):
    if barcode in inventory.keys():
        item = inventory[barcode]
        item["stock level"] -= amount
        if item["stock level"] < 0:
            item["stock level"] = 0
        return True     # there was an item to update
    return False        # there was no match on the barcode

.. and so on.

ok this is my " Catalog"

Cpus I5-3570k 218$
I7-3570K 493$

Cpu Cooler CM Hyper 212 Evo 40$

GPUs AMD HD 7950 330$
GTX Titan 999$
Case Haf 912 Advance 49$
RAM 8GB (2x4GB) 1600 67$
Mother Board MSI Z77A-G43 139$
SSD ADATA 120GB 6GB/S 99$
Mouse Razer Deathader 48$

The stock canot be ander 5.
and if its under 5 items in stock so i need to get a massage that i need to buy more of this ;D

tnx for helping

OK, well, I wouldn't put a lot of weight on the specific inventory items; in fact, if you do this correctly, they shouldn't even come up in the program itself. Why not? Because you'll almost certainly want to store them in a data file of some sort so that they persist even between runs of the program, or better still, in an actual database of some kind. Even if that proves too much to deal with, you can still have them in a separate file from the body of the program, which could be imported into the code proper at run time. The looser you couple the data and the code - that is to say, the more you can seperate the actual data from the data structures and the code that work on it - the better off you'll be.

Ok so how i am continuo next ? what should i do

how your program works ?

What, you mean the code I posted earlier? That was just an example. You'll need to write your own code for your project.

I do want to know though, are you familiar with classes in Python, or you didn't get that far into the subject at your school? Because if you can use classes, this will be easy. Also, a modular/function approach will be required if classes are not available.

i have a problem . My teacher told me i can use only loops i cant use list,dict,and all this type ;(

Seriously? That can't be right - the project as described can't be done without some sort of data structures. It simply is isn't realistic to do it that way - you would need to hard-code each item as a separate variable, then hardcode its price as another set of separate variables, its barcode as yet another set, and so on. No programmer in their right mind would do it like that.

Walk out of this class; your instructor doesn't know how to program, and you'll be better off finding another instructor.

Well, maybe your teacher forbade you to use some functions which are build in from lists or dictionaries such as remove, insert, so that he'll force you to start thinking on how to implement some of the functionalities, but I highly doubdt that he forbade you to use actual lists... That's idiotic.

tprice=0.0
bc='bc'

uname='admin'
upass='1234'
Choice=input("""1-Wach a catalog
2-Go to cash register""")
if Choice==1:
    print"""
 ----------i5-3570k---------
| Price - 218$              |
| Product Code - 04353736   |
| Discriptions -            |
|___________________________|

 ----------i7-3570k---------
| Price - 493$              |
| Product Code - 04322575   |
| Discriptions -            |
|___________________________|

 -----CM Hyper 212 Evo------
| Price - 40$               |
| Product Code - 04201221   |
| Discriptions -            |
|___________________________|

 --------AMD HD 7950--------
| Price - 330$              |
| Product Code - 03745501   |
| Discription -             |
|___________________________|

 ---------GTX Titan---------
| Price - 990$              |
| Product Code - 01323661   |
| Discriptions -            |
|___________________________|

 ------Haf 912 Advance------
| Price - 49$               |
| Product Code - 04221214   |
| Discription -             |
|___________________________|

 -----8GB (2x4GB) 1600------
| Price - 67$               |
| Product Code - 04664125   |
| Discription -             |
|___________________________|

 --------MSI Z77A-G43-------
| Price - 139$              |
| Product Code - 07743210   |
| Discriptions -            |
|___________________________|

 -----ADATA 120GB 6GB/S-----
| Price - 99$               |
| Product Code - 01236252   |
| Discriptions -            |
|___________________________|

 ------Razer Deathader------
| Price - 04625152          |
| Product Code - 04625152   |
| Discription -             |
|___________________________|
"""

if Choice==2:
    username=raw_input("""Login
    User name:""")
    if username!=uname:
            print "Wrong username"

    if username==uname:


        password=raw_input("""Login
    Password:""")
        if password!=upass:
            print "Wrong password"
        if password==upass:

            while bc!=00000000:

                bc=input("Please scan a barcode")

                if bc==04353736:

                    price=218
                    name="i5-3570k"
                    stock=23

                if bc==04322575:

                    price=493
                    name="i7-3570k"
                    stock=8

                if bc==04201221:

                      price=40
                      name="CM Hyper 212 Evo"
                      stock=7


                if bc==03745501:

                    price=330
                    name="AMD HD 7950"
                    stock=95


                if bc==01323661:

                  price=990
                  name="GTX Titan"
                  stock=13


                if bc==04221214:

                    price=49
                    name="Haf 912 Advance"
                    stock=18


                if bc==04664125:

                    price=67
                    name="8GB (2x4GB) 1600"
                    stock=19


                if bc==04664125:

                    price=67
                    name="8GB (2x4GB) 1600"
                    stock=57


                if bc==07743210:

                    price=139
                    name="MSI Z77A-G43"
                    stock=7


                if bc==01236252:

                    price=99
                    name="ADATA 120GB 6GB/S"
                    stock=13




                if bc==04625152:

                    price=48
                    name="Razer Deathader"
                    stock=6



                tprice=tprice+price
                print "",price

        tprice=tprice-price

        print "Total is-",tprice,"$"





This is what i did for now and its working like i want ;D (almost) when i input 0000000 in barcodes its giving me the lest price twice

but i dont know how tomake the stock level so it will save after.

I'm not sure if this will help but i wrote this for you, read through it and it might give you some ideas for your project even if you cant use some of it:

import time

class main(object):
    def __init__(self):
        self.login()
        self.MainLoop()

    def login(self):
        self.USERS = ["admin", "admin"]
        self.user = raw_input("Username: ")
        self.password = raw_input("Password: ")
        while True:
            if self.user in self.USERS[0] and self.password in self.USERS[1]:
                print("Welcome " + self.USERS[0])
                break
            else:
                print("Invalid login.")
                self.user = raw_input("Username: ")
                self.password = raw_input("Password: ")

    def MainLoop(self):
        self.items = {001: 0.50, 002: 0.20}
        self.moneyDew = 0
        print("NEW CUSTOMER")
        while True:
            self.whatToDO = int(raw_input("Item Number: "))
            self.moneyDew += self.items[self.whatToDO]
            self.conf = raw_input("ADD/EXIT: ")
            if self.conf == "ADD":
                pass
            elif self.conf == "EXIT":
                self.moneyDew = str(self.moneyDew)
                print("Total: " + self.moneyDew)
                time.sleep(2)
                print("Printing recipt...")
                time.sleep(3)
                print("Done!")
                break
                self.MainLoop()

start = main()

Sorry if this doesn't help. But hopefully you learn some things from it.

  • mat

tnx . its look helpful but i cant use def. only loops
Here is my code

import time
import datetime
today = datetime.date.today()

tprice=0.0
bc='bc'


si5=8
si7=8
shyper=7
samdhd=9
stitan=6
shaf=8
sram=5
smsi=7
sada=10
srazer=6


uname='admin'
upass='1234'

Choice=input("""1-Wach a catalog
2-Go to cash register(Need to login into Saler Account""")
if Choice==1:
    print"""
|--------------------i5-3570k--------------------|
| Price - 218$                                   |
|                                                |
| Product Code - 04353736                        |
|                                                |
| Discriptions - Intel Core i5-3570K Quad-Core   |
|                Socket LGA1155, 3.4Ghz, 6MB L3  |
|                Cache, 22nm (Retail Boxed) Gen3 |
|________________________________________________|

 --------------------i7-3970X--------------------
| Price - 1009.99$                               |
|                                                |
| Product Code - 04322575                        |
|                                                |
| Discriptions - Intel Core i7-3970X Extreme     |
|                Edition Six Core Socket LGA2011,|
|                3.5 GHz, 15MB L3 Cache.         |
|________________________________________________|

 ---------------CM Hyper 212 Evo-----------------
| Price - 34.82$                                 |
|                                                |
| Product Code - 04201221                        |
|                                                |
| Discriptions - Cooler Master Hyper 212 EVO     |
|                Direct Heat Pipe CPU Cooler R2, |
|                Intel 2011/1366/1156/1155/775   |
|                and AMD FM1/AM3+/AM3/AM2+/AM2   |
|                                                |
|________________________________________________|

 -------------Gigabyte AMD HD 7950---------------
| Price - 330$                                   |
|                                                |
| Product Code - 03745501                        |
|                                                |
| Discription -  Gigabyte (GV-R795WF3-3GD) AMD   |
|                Radeon HD 7950 3GB GDDR5        |
|                900 MHz Clock, 5000 MHz Memory  |
|                PCI-Express 3.0, Dual-Link DVI, |
|                Dual Mini Display Port, HDMI    |
|________________________________________________|

 -----------------Zotac GTX Titan----------------
| Price - 990$                                   |
|                                                |
| Product Code - 01323661                        |
|                                                |
| Discriptions - Zotac NVIDIA GeForce GTX TITAN  |
|                6GB GDDR5 837 MHz Clock, 6008   |
|                MHz Memory PCI-Express 3.0,     |
|                Dual Display DVI, , HDMI        |
|________________________________________________|

 ---------Cooler Master HAF 912 Advance-----------
| Price - 89.75$                                 |
|                                                |
| Product Code - 04221214                        |
|                                                |
| Discription - Cooler Master HAF 912 Advance Mid|
|               Tower ATX Case                   |
|________________________________________________|

 ------G.SKILL Ripjaws Z + Fan Series 64GB ------
| Price - 639.95$                                |
|                                                |
| Product Code - 04664125                        |
|                                                |
| Discription - G.SKILL Ripjaws Z + Fan Series   |
|               64GB (8x8GB) DDR3 2400MHz CL10   |
|               Quad Channel Kit                 |
|________________________________________________|

 -----------------MSI Z77A-G43--------------------
| Price - 139$                                   |
|                                                |
| Product Code - 07743210                        |
|                                                |
| Discriptions - MSI Z77A-G43 Socket 1155 Intel  |
|                Z77 Chipset Dual Channel DDR3   |
|                2800(O.C.) MHz, 2x PCI-Express  |
|                x16 GLAN, 4x SATA 3.0Gb/s, 2x   |
|                SATA 6.0Gb/s, 2x USB 3.0, 6x    |
|                USB 2.0 DVI/VGA/HDMI, ATX       |
|________________________________________________|

 -----------------ADATA 120GB 6GB/S--------------
| Price - 99$                                    |
|                                                |
| Product Code - 01236252                        |
|                                                |
| Discriptions - ADATA S510 120GB 2.5" SATA 6Gb/s|
|                Solid Sate Drive (SSD),         |
|                Read: 550MB/s Write: 510MB/s    |
|________________________________________________|

 ---------------Razer Deathadde------------------
| Price - 48                                     |
|                                                |
| Product Code - 04625152                        |
|                                                |
| Discription - Razer Deathadder Black 3500 High |
|               Precision 3.5G                   |
|________________________________________________|
"""

if Choice==2:
    username=raw_input("""Login
    User name:""")
    if username!=uname:
            print "Wrong username"

    if username==uname:


        password=raw_input("""Login
    Password:""")

        if password!=upass:
            print "Wrong pass word"
        if password==upass:
            print """
        ______________________________
       |   ***   ***    ********      |
       |   ****  ***       **         |
       |   *** * ***       **         |
       |   ***  ****       **         |
       |   ***  ****ew     **ech      |
       ********************************
       |Adres: 126 Alfred Ave. Toronto|
       |Tel: 697-648-2088             |
       |Fax : 880-284-6796            |
       |       www.NewTech.ca         |
       |______________________________|
       |          Recipet             |
       |______________________________|"""
            while bc!=123456789:

                bc=input("Please scan a barcode")

                if bc==04353736:

                        price=218
                        name="i5-3570k"
                        si5=si5-1
                        stock=si5







                if bc==04322575:

                        price=1009.99
                        name="i7-3970X"
                        si7=si7-1
                        stock=si7

                if bc==04201221:

                      price=34.82
                      name="CM Hyper 212 Evo"
                      shyper=shyper-1
                      stock=shyper


                if bc==03745501:

                    price=330
                    name="Gigabyte AMD HD 7950"
                    samdhd=samdhd-1
                    stock=samdhd


                if bc==01323661:

                  price=990
                  name="Zotac GTX Titan"
                  stitan=stitan-1
                  stock=stitan


                if bc==04221214:

                    price=89.75
                    name="CM HAF 912 Advance"
                    shaf=shaf-1
                    stock=shaf

                if bc==04664125:

                    price=639.95
                    name="G.SKILL Ripjaws Z+Fan Series 64GB"
                    sram=sram-1
                    stock=sram




                if bc==07743210:

                    price=139
                    name="MSI Z77A-G43"
                    smsi=smsi-1
                    stock=smsi


                if bc==01236252:

                    price=99
                    name="ADATA 120GB 6GB/S"
                    sada=sada-1
                    stock=sada



                if bc==04625152:

                    price=48
                    name="Razer Deathadde"
                    srazer=srazer-1
                    stock=srazer


                if stock>0:
                    tprice=tprice+price


                    print """       -""", name,""".....""",price ,"$ "

                if stock<=0:
                    print """       -Soory this itam is out of stock!
        Come back latter if you want this"""



            tprice=tprice-price
            tax=tprice*0.13

            fprice=tprice+tax
            print """       |______________________________|"""
            print """       - SubTotal.....""",tprice ,"$ "
            print """       - Tax.............""",tax ,"$"
            print """       -Total.........""",fprice ,"$ "
            paid=input("How mach do you paid?")

            if paid>=fprice:

                change=paid-fprice

                ct=round(change/0.5) * 0.5


                print """       |______________________________|"""
                print """       - Paid........""",paid ,"$ "
                print """       - Change........""",ct ,"$ "
                print """       |           Thank you ;)       |"""
                print """       |         Have a nice day      |"""
                print """       |______________________________|"""
                print "       |     ",\
                time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()),"    |"
                print """       |______________________________|









   ***Messege to Stor manager***"""
                if si5<5:
                    print "Get more i5"

                elif si7<5:
                    print "Get more i7"

                elif shyper<5:
                    print "Get more hyper"


                elif samdhd<5:
                    print "Get more HD 7950"

                elif stitan<5:
                    print "get more titan"


                elif shaf<5:
                    print "Get more hsf"

                elif sram<5:
                    print"get more rams"


                elif smsi<5:
                    print "get more msi"

                elif sada<5:
                    print " get more afa"

                elif srazer<5:
                   print "get more razzer"




            if paid<fprice:
                print """       |I'm sorry but you dont the    |"""
                print """       |money to pay, please come back|"""
                print """       |when you will get a monye     |"""
                print """       |to buy those items !!         |"""
                print """       |______________________________|"""
                print """       |           Thank you ;)       |"""
                print """       |         Have a nice day      |"""
                print "       |     ",\
                time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()),"    |"
                print """       |______________________________|




                """

uhm, forbade you to use def (functions)? i don't think thats right, and not using lists/dicts/etc. can't be right either.

dict, list, class, etc. would get rid of all of those if statements, and allow for readable code.
Plus a program without a function definition makes you repeat yourself too much.
I'm not a code-golfer, but this whole thing could be done in less than 100 lines, probably less than 50.

What school is this?

If someone enters a wrong user name, or password, or bc, nothing happens. It should be something along the lines of

if Choice==2:
    username = ""
    while username != uname:
        username=raw_input("Login User name: ")

This is definitely a list exercise IMHO and whould be much easier with dictionaries. The point is that you want one container for everything. Then what is printed and what is used when the customer chooses something come from the same place. If you make an error entering somewhere, it may print one price and charge another. Also, when you want to make changes you should be able to change one list/dictionary/file and have it reflect everywhere. This is an example using the first two computers.

##     number, price, bar_code, qty_on_hand, description (one line per)
products_list = [
    ["i5-3570k", 218, '04353736', 5, "Intel Core i5-3570K Quad-Core",
     "Socket LGA1155, 3.4Ghz, 6MB L3", 
     "Cache, 22nm (Retail Boxed) Gen3"],
    ["i7-3970X", 1009.99, '04322575', 5, "Intel Core i7-3970X Extreme",
     "Edition Six Core Socket LGA2011", "3.5 GHz, 15MB L3 Cache"]]

##---------- print all products
##        you could use as list of lists as here as well
empty_line = "|" + " "*49 + "|"
dashes = "|" + "-"*49 +"|"

for product in products_list:
    ## print dashes and product number
    output = "|--------------------" + product[0]
    output += "-"*(50-len(output)) + "|"
    print output

    ## print price
    output = "| Price - " + str(product[1]) + "$"
    output += " "*(50-len(output)) + "|"
    print output
    print empty_line

    ## print product code
    output = "| Product Code - " + product[2]
    output += " "*(50-len(output)) + "|"
    print output
    print empty_line


    ## print all description lines
    output = "| Description - "
    start = len(output)-1
    description = product[4:]
    for each_line in description:
        output += each_line
        output += " "*(50-len(output)) + "|"
        print output
        output = "|" + " "*start

    print empty_line
    print dashes
    print

##----------  Now simulate a choice
total_purchase = 0
bc = '04322575'
item_found=False
for item in products_list:
    if bc == item[2]:
        item_found = True
        price=item[1]
        name=item[0]
        item[3] -= 1      ## reduce inventory
        total_purchase += price
if not item_found:
    print "item is not on file"

## buy another one
bc = '04322575'
for item in products_list:
    if bc == item[2]:
        price=item[1]
        name=item[0]
        item[3] -= 1      ## reduce inventory
        total_purchase += price

print "Total purchase price =", total_purchase
print "\nInventory on hand"
for product in products_list:
    print "%10s = %d" % (product[0], product[3])

i know that it will be easyer with list and dif and all this staff but my teacher told me to use only if . no DIF DICT or LIST. so i am done my program it wasnt so hard how i was thinking . ;D thnx for evry body who helped

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.