Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from datetime import datetime
- components = ("Processor",
- "RAM",
- "Storage",
- "Screen",
- "Case",
- "USB ports")
- choices = ("P3",
- "P5",
- "P7",
- "16GB",
- "32GB",
- "1TB",
- "2TB",
- "19\"",
- "23\"",
- "Mini Tower",
- "Midi Tower",
- "2 ports",
- "4 ports")
- prices = (100, 120, 200, 75, 150, 50, 100, 65, 120, 40, 70, 10, 20)
- stock = [3, 2, 1, 1, 5, 2, 0, 5, 6, 7, 8, 0, 8]
- def output_component_info(comp_type, start, stop):
- """
- Abstract implementation of procedure to display component info.
- Uses zip() to associate component and price (from components, prices) using
- list comprehension. This is so component and proce can be outputted on
- same line.
- :param str comp_type: Section header to specify component type.
- :param int start: Value to be used as the list slice start index.
- :param int stop: Value to be used as the list slice stop index.
- """
- print(comp_type)
- print("Your choices (with prices) are: ")
- for choice, price in zip(choices[start:stop], prices[start:stop]):
- print(choice, str(price))
- def capture_component_choice(start, stop):
- """
- Abstract implementation of function to capture user's choice for each
- component type.
- :param int start: Value to be used as the list slice start index.
- :param int stop: Value to be used as the list slice stop index.
- :return str component_choice: Section header to specify component type.
- """
- component_choice = input("Make your choice: ")
- component_choice = validate_component_choice(component_choice, start, stop)
- while not check_stock_level(component_choice):
- print("That component is out of stock, please try again.")
- component_choice = input("Make your choice: ")
- component_choice = validate_component_choice(component_choice, start, stop)
- print()
- return component_choice
- def validate_component_choice(component_choice, start, stop):
- """
- Performs validation on users' input for component_choice.
- Validation is case insensitive, however onece validated the inputted value,
- even if of different case from original choice, will persist.
- :param str component_choice: The value to be validated.
- :param int start: Value to be used as the list slice start index.
- :param int stop: Value to be used as the list slice stop index.
- :return str component_choice: Validated version of user inputted value.
- """
- while component_choice.lower() not in get_lowercase_components()[start:stop]:
- print("That is not a valid choice.")
- component_choice = input("Please choose again: ")
- return component_choice
- def check_stock_level(component_choice):
- """
- Checks to see of the chosen component is in stock.
- :param str component_choice: The component to check:
- :return boolean: In stock status of the component.
- """
- if stock[get_lowercase_components().index(component_choice.lower())] > 0:
- return True
- else:
- return False
- def get_lowercase_components():
- """
- Returns a version of the global data structure choice, with all values
- formatted to lowercase.
- Can be used for validation, amoungst other things.
- :return list components: Reformatted version of choice.
- """
- components = []
- for component in choices:
- components.append(component.lower())
- return components
- def get_componet_price(component_choice):
- """
- Returns the price associated with the passed component.
- Uses the index of component_choice in choices, to find the associated
- vaule from global data structure, prices.
- :param str component_choice: The value to find the associated value from prices.
- :return int: The associated value from prices
- """
- return prices[get_lowercase_components().index(component_choice.lower())]
- def update_estimate(estimate, component_choice, component_price):
- """
- Updates the estimate with the most recent component details.
- Appends parameters component_choice and component_price to the end of
- param estimate. Adds parameter component_price to running total at
- parameter estimate[0].
- :param list estimate: A list conatining the estimate so far.
- :param str component_choice: The component choice to be added to estimate.
- :param str component_price: The component price to be added to running total.
- :return list estimate: The updated estimate.
- """
- estimate.append(component_choice)
- estimate.append(component_price)
- estimate[0] += component_price
- return estimate
- def generate_estimate(estimate, estimate_num):
- """
- Generates an estimate for the user.
- Prints component names and prices, by making temporary lists from param
- estimate begining at indexes 1 and 2 (for component names, component prices
- respectively), then every other index (step count of 2) thereafter. Then
- uses zip() to associate component name and proce using list comprehension.
- TODO: improve presentation using .format() to align output in columns.
- :param list estimate: The data needed to generate the estimate
- """
- date = datetime.today().strftime('%Y.%m.%d')
- estimate_num = date + '.' + str(estimate_num)
- print("ESTIMATE")
- print("Estimate number: " + estimate_num)
- print("Your choices (with prices) are: ")
- component_names = estimate[1::2]
- component_prices = estimate[2::2]
- for choice, price in zip(component_names, component_prices):
- print(choice, str(price))
- print("Total cost is: " + str(estimate[0] * 1.2))
- def main():
- estimate_num = 0
- while True:
- estimate = [0]
- estimate_num += 1
- output_component_info("Processor options", 0, 3)
- processor_choice = capture_component_choice(0, 3)
- processor_price = get_componet_price(processor_choice)
- estimate = update_estimate(estimate, processor_choice, processor_price)
- output_component_info("RAM options", 3, 5)
- ram_choice = capture_component_choice(3, 5)
- ram_price = get_componet_price(ram_choice)
- estimate = update_estimate(estimate, ram_choice, ram_price)
- output_component_info("Storage options", 5, 7)
- storage_choice = capture_component_choice(5, 7)
- storage_price = get_componet_price(ram_choice)
- estimate = update_estimate(estimate, storage_choice, storage_price)
- output_component_info("Screen options", 7, 9)
- screen_choice = capture_component_choice(7, 9)
- screen_price = get_componet_price(screen_choice)
- estimate = update_estimate(estimate, screen_choice, screen_price)
- output_component_info("Case options", 9, 11)
- case_choice = capture_component_choice(9, 11)
- case_price = get_componet_price(case_choice)
- estimate = update_estimate(estimate, case_choice, case_price)
- output_component_info("USP ports options", 11, 13)
- usb_choice = capture_component_choice(11, 13)
- usb_price = get_componet_price(usb_choice)
- estimate = update_estimate(estimate, usb_choice, usb_price)
- generate_estimate(estimate, estimate_num)
- go_again = input("Do you want to generate another estimate (y/n): ")
- if go_again.lower() == 'n':
- break
- print()
- #TODO
- #main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement