@WiNDfaLLx/

the mock prerelease

Python

if you see this comment your favourite colour on the group chat

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""ᴄᴏᴅᴇᴅ ʙʏ ⟁ᴛʀɪ⟁ ᴠɪʜᴍ
♡ᴄᴏᴍᴘʟᴇᴛᴇʟʏ ᴏᴘᴇɴ ꜱᴏᴜʀᴄᴇ!♡"""
#NOTE: comments generally talk about the line BELOW them. Okay?
#ALSO: I use array/list and item/element/choice interchangeably.
#PLUS: this was finished at 4am on a Saturday. EXPECT weird grammar.

#it's a good habit to import first. We'll need this thing somewhere. Eventually.
from datetime import datetime

#general starting/empty variable settings and imports. I like to do these at the start, since then it's nice and neat.
now = datetime.now()
estimatenumber = 0
ordersplaced = []
ordervaluetotal = 0
#and the one you've all been waiting for - the *3 dimensional array*. It's a list of all component types, choices and attributes (i.e. name/price/stock).
allcomponents = [["Processor", ["p3", 100, 10], ["p5", 120, 10], ["p7", 200, 10]], ["RAM", ["16 GB", 75, 10], ["32 GB", 150, 10]], ["Storage", ["1 TB", 50, 10], ["2 TB", 100, 10]], ["Screen", ["19\"", 65, 10], ["23\"", 120, 10]], ["Case", ["Mini Tower", 40, 10], ["Midi Tower", 70, 10]], ["USB Port", ["2 ports", 10, 10], ["4 ports", 20, 10]]]
#dimension 1: [processor, RAM, storage, screen, case, ports] -> component categories
#dimension 2: ["component name", choice, choice, choice] -> choices for each component
#dimension 3: ["choice name", $price, #stock] -> attributes for each choice

#right, let's get started! I assumed "end of day" meant after 6 (closing time).
#this monstrous while loop lets customers endlessly make estimates and orders right from opening time (when the program is run) to when it shuts for the day.
while int(now.hour) < 18:
  #just a tiny bit more variable (re)setting.
  customerchoice = ""
  customercomponents = []
  componenttotal = 0
  orderit = ""
  customerdetails = ["name", "phone number"]

  input("Press enter to start building your custom-built PC! \nThis program will give you an estimated price and allow you to place an order.")

  #makes the estimate number unique... by setting it as the next number up, lol.
  estimatenumber += 1

  #this loops through each component category, printing the options in it and asking the customer for their choice.
  for component in allcomponents:
    #the first (0th) element in a component array is the component category name (see line 18). It's printed out as a neat heading.
    print("\n" + component[0], "options:")
    #this odd-looking indexing is called splicing, and uses two indexes (list item numbers) and a colon to get everything in betweeN. You can use it on strings - e.g. "computer"[3:7] gives "pute" - and also on lists to give certain items! Leaving the second index blank implies the splice goes up to the end of the list - so [1:] basically gives everything except item 0. These are all our choice arrays, (see line 18), which are printed slightly differently.
    for choice in component[1:]:
      #the first item in a choice array is its name (see line 19), which is printed for the customer to choose. 
      #list.index(item) just returns the item's number in the list. Since I'm iterating through an array rather than using range(), it's the only way to print numbered lists.
      print(str(component.index(choice)) + ". ", choice[0], end="  ")
    #this code asks the customer the number of the choice they want to enter.
    #not in is way more efficient than != "1" and != "2" and != "3". Less typing!
    while customerchoice not in ["1", "2", "3"]:
      #if there aren't three choices, don't type 3. I shall fix this later. I am a lazy person.
      #also, if I put one more if loop, I hit this "cyclomatic complexity" limit.
      customerchoice = input("\nPlease enter the number of the " + component[0] + " option you would like to choose (a single digit):")
    #just an input, pretty simple.
    customerchoice = int(customerchoice)
    #decided to save the customer choices as the choice number/index (see line 18) rather than the name, e.g. 2 for option 2.
    #this allows me to easily access the name, cost and stock later, with the list.
    customercomponents.append(customerchoice)
    #the double [][] is how to access a multidimensional list. [customerchoice] gives the index of the choice array (see line 18). [1] gets the second element of this array, which is the price (see line 19). Indexing starts from zero!
    componenttotal += component[customerchoice][1]
    #although it's a common error, you do not need to subtract one from customerchoice here, since element 0 is the component category title. The second element of the array, with index 1, is actually choice 1!

  print("\nEstimate Number:", estimatenumber)
  print("Your custom-built PC will have these components-")

  #this for loop runs through each component category list (see line 17, it's all 7 of those.)
  for component in allcomponents:
    #alright, so this was some horrible mess I got through trial and error.
    #component[0] is the component name (see line 18).
    #what I am trying to get is the name of the cchoice of component the customer has chosen.
    #this requires accessing a multidimensional list (see line 56).
    #essentially, this is component[customer choice for that component][0].
    #the 'customer choice for that component' bit is where it gets messy. customercomponents is a list, where the choice numbers are in the same order as the allcomponents list (line 17) - the processor choice is first, etc.
    #since my for loop runs through the *items* of a list rather than with range() and numbers, I need to first use index() to get a number to access the customercomponents list with, then stick that number into that list.
    #Yeah. That's it.
    print(component[0] + ":", component[customercomponents[allcomponents.index(component)]][0], "at $" + str(component[customercomponents[allcomponents.index(component)]][1]))

  #just added this line for some extra detail.
  print("Build Cost: $" + str(componenttotal*0.2))

  #this should be simple enough to understand...
  print("\nIts estimated price is", str(componenttotal*1.2) +  "$")

  while orderit not in ["Y", "N"]:
    orderit = input("\nPlace this order? Please enter Y or N:")
    orderit = orderit.upper()

  #gotta make sure they actually ordered it!
  if orderit == "Y":
    for component in allcomponents:
      #this checks whether the components are in stock.
      #yes, it's the gross list index mess from line 65 again. I hate it too.
      #component[number][2] is the stock level (see line 19).
      if component[customercomponents[allcomponents.index(component)]][2] < 1:
        print("\nSorry, the", component[customercomponents[allcomponents.index(component)]][0], component[0].lower(), "is out of stock!")
        #orderit is called a flag variable, saying whether the order's valid. 
        #it works well (unlike 'break', which can't get out of the main loop!)
        orderit = "N"
  else:
    print("\nYour order has been cancelled.\n\n")

  #now have to check orderit again to see whether it all was in stock.
  if orderit == "Y":
    #using my copy-pasted apocalyptic list atrocity to decrease stock.
    for component in allcomponents:
      component[customercomponents[allcomponents.index(component)]][2] -= 1
    
    #an intuitive bit: adding the estimate number to the list of order numbers.
    ordersplaced.append(customerdetails)

    #gettin some customer details. Flip validation/verification, lol.
    #sigh. I absolutely bloody love lists, don't I?
    customerdetails[0] = input("\n\nPlease enter your name:")
    customerdetails[1] = input("Please enter a phone number, so we may contact you about this order:")

    #ok. Now, this is to help with the end of day summary thing.
    ordervaluetotal += (componenttotal*1.2)

    #alright, I gotta print this twice.
    for repeat in range(2):
      print("\nOrder Number", estimatenumber)
      #here's the date. The %s are a formatting thing I got from Codecademy.
      print("Order placed for", customerdetails[0], "contactable at", customerdetails[1], "on date", "%s╱%s╱%s" % (now.day, now.month, now.year))
      #this is literally a copy of line 73. Another list mess. What a lisdaster!
      for component in allcomponents:
        print(component[0] + ":", component[customercomponents[allcomponents.index(component)]][0], "at $" + str(component[customercomponents[allcomponents.index(component)]][1]))
      print("Order total: $" + str(componenttotal*1.2), "\n\n")
  
  #updating the time - or the loop will never end!
  now = datetime.now()

#boom, done! Alright, that wasn't *that* bad...

#now for the end of day summary bit.
#len() on a list gives the total number of items on it.
print("Total number of orders made:", len(ordersplaced))
#this runs through each component category...line 17...yeah.
for component in allcomponents:
  #this is an exact copy of line 39, printing the component category title.
  print("\n" + component[0], "options:")
    #and this is that list splice that you encountered on line 41.
  for choice in component[1:]:
    #all starting stock numbers were 10 - subtract to get how many were sold.
    print("Total number of", choice[0], "components sold:", (10-choice[2]))
print("\nTotal value of orders placed: $" + str(ordervaluetotal))

#and that's it! Well done for making it this far! You're alive!
#if it's still hard to get (sorry, I'm not the best explainer...) literally just talk to me! DM me on WhatsApp or something. I'll try to explain it.
#Or, if you're the figure-everything-out-yourself╱I-love-suffering type...try disabling large sections of code using """triple quotes""", which are like multi-line comments. With the help of a few print statements, you'll then be able to see exactly what specific sections do without having to delete it all!

#p.s. when did I unofficially become a computer science teacher? >_<
#I don't even have to do anything and it's already too much responsibility lol