Created
April 4, 2019 18:57
-
-
Save actuallyjamez/6b89306b98066fd0425347b5dcb39655 to your computer and use it in GitHub Desktop.
lol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # Date: 18/03/2019 | |
| # Module: Introduction to Programming. | |
| # Description: This file is a script used to perform various calculations on the prices of a set of given items. | |
| # | |
| # Note: I've used docstrings for functions, but I wasn't sure how many comments to include in the main script. I | |
| # think most of the code is self explanatory and I didn't want to restate the obvious as per the commenting guidelines. | |
| # | |
| def decision(question): | |
| """ | |
| Asks the user a question and checks for y/n while handling any exception cases without exceptions . | |
| :param question: Name of question | |
| :type question: str | |
| :return: Users decision | |
| :rtype: bool | |
| """ | |
| while True: | |
| decision_input = input('{0} (y/n): '.format(question)) | |
| if len(decision_input) == 1 and decision_input.isalpha(): | |
| if decision_input == 'y': | |
| return True | |
| elif decision_input == 'n': | |
| return False | |
| def validate_string_float(value): | |
| """ | |
| I couldn't find a string method that validated floats, so I made my own. | |
| takes a given string, splits by the first '.' (if a float) and compares with isdigit(). | |
| :param value: Float to validate | |
| :type value: str | |
| :return: True or False | |
| :rtype: bool | |
| """ | |
| if value.isdigit(): # check for int | |
| return True | |
| elif '.' in value: | |
| # iterate through a list generated from spiting the given string by decimal point into the list. | |
| for i in value.split('.', 1): | |
| if not i.isdigit(): | |
| return False | |
| return True | |
| else: | |
| return False | |
| def get_tax(item): | |
| """ | |
| Calculates the amount of tax on an item. | |
| :param item: Cost of the item | |
| :type item: float | |
| :return: Total tax for the item | |
| :rtype: float | |
| """ | |
| return item * 0.20 # tax = 20% | |
| if __name__ == '__main__': | |
| item_count = 0 | |
| total_price = 0 | |
| while True: | |
| new_item = input('Price of item: £') | |
| if validate_string_float(new_item): | |
| total_price += float(new_item) | |
| item_count += 1 | |
| if not decision('Add another item?'): | |
| break | |
| print('\nNumber of items: {0}'.format(item_count)) | |
| print('Average cost per item: £{0:.2f}\n'.format(total_price / item_count)) | |
| print('Total Cost: £{0:.2f}'.format(total_price)) | |
| print('Total Tax: £{0:.2f}\n'.format(get_tax(total_price))) | |
| print('Net Price Tax: £{0:.2f}'.format(total_price + get_tax(total_price))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment