Simple IT shop with python (python noob)

Hello! So i’ve just started my study (cybersecurity) and i’m having a lot of trouble with Python. I need to make a simple shop that list 8 IT related products, keeps track of the products (how much is left) and tells the user that a product is out of stock and print how much is left. I’m especially having trouble with creating a loop. I def need to learn more but i was hoping i could get some advice. It’s in Dutch so ill translate some important stuff:
voorraad = stock
keuze_klant = choice_customer
Wat wilt u kopen? = what do you want to buy
Dit product hebben we helaas niet meer op voorraad = we dont have that product in stock

Also the code is really messy but again this is my second/third week of having Python so bear with me.

#welkom text + whats in stock
print ("Hallo welkom bij IT4you. De beste ICT specialist van Haarlem!\n")
print("Wij hebben een groot assortiment aan producten:")
print("Computers\n" "Laptops\n" "Processors\n" "Videokaarten\n" "Beeldschermen\n" "Toetsenborden\n" "Koptelefoons\n" "Kabels\n")

stock
computers = 32
laptops = 63
processors = 50
videokaarten = 0
toetsenborden = 84
beeldschermen = 47
koptelefoons = 112
kabels = 225
voorraad = computers, laptops, processors, videokaarten, beeldschermen, koptelefoons, kabels

#stop letter for user
print("Typ q om te stoppen")

while True:
    keuze_klant = input ("Wat wilt u kopen? ")
    if keuze_klant == ("q"):
        print(voorraad)
        break
    if keuze_klant == "laptop":
            laptops -= 1
    elif laptops == 0:
        print ("Dit product hebben we helaas niet meer op voorraad")
    if keuze_klant == "computer":
            computers -= 1
    elif computers == 0:
        print ("Dit product hebben we helaas niet meer op voorraad")
    if keuze_klant == "processor":
            processors -= 1
    elif processors == 0:
        print("Dit product hebben we helaas niet meer op voorraad")
    if keuze_klant == "videokaart":
            videokaarten -= 1
    elif videokaarten == 0:
        print("Dit product hebben we helaas niet meer op voorraad")
    if keuze_klant == "toetsenbord":
            toetsenborden -= 1
    elif toetsenborden == 0:
        print("Dit product hebben we helaas niet meer op voorraad")
    if keuze_klant == "koptelefoon":
            koptelefoons -= 1
    elif koptelefoons == 0:
        print("Dit product hebben we helaas niet meer op voorraad")
    if keuze_klant == "kabel":
            kabels -= 1
    elif kabels == 0:
        print("Dit product hebben we helaas niet meer op voorraad")
    else:
        print("Bedankt en tot ziens!")
        print("Huidige voorraad: ", voorraad)

I forgot to tell whats going wrong but its like it’s not even paying attention to the if statements and elif, for example:
if keuze_klant == “kabel”:
kabels -= 1

elif kabels ==0:
print (“we dont have this in stock”)

Looks like its just printing what ive put in but doesn’t actually look if kabels == 0 or actually -1 a product when the user typs in kabel

It’s the same thing repeatedly, so let’s just step through one product type as if we were the computer:

In the first lines, we ask if the customer wants a laptop, and if he does, we take one out of stock. But if he doesn’t want a laptop (this is what elif means) then we check the stock and if we’re out of laptops we say so. But what does the customer care about that, since he doesn’t want a laptop?

The right structure is something like:

    if keuze_klant == "laptop":
        if laptops == 0:
            print ("Dit product hebben we helaas niet meer op voorraad")
        else:
            laptops -= 1
    elif keuze_klant == "computer":
        # ... same again
    elif keuze_klant == "processor":
        ...
    ...
    else:
        print("Bedankt en tot ziens!")

Repetitive code is a sign you should be using a function or a loop, but make it work the way you wrote it before that improvement.

2 Likes

Thank you so much! I know why now.