Skip to content

Instantly share code, notes, and snippets.

@Double-A-92
Created March 1, 2017 09:31
Show Gist options
  • Save Double-A-92/778b7fc249d757c6ac343dee19fffed5 to your computer and use it in GitHub Desktop.
Save Double-A-92/778b7fc249d757c6ac343dee19fffed5 to your computer and use it in GitHub Desktop.
# - Global Constants -
SALES_TAX = 35 # Tax paid when selling (in %)
CURRENCY_UNIT = "USD"
# - Input Handling Functions -
def input_total_cost():
"""Asks the user how much he spent for the products in total"""
total_cost_raw = input("How much did everything cost? ")
total_cost = total_cost_raw.replace(',', '') # Not quite sure what this is supposed to do
return int(total_cost) # Why do you want Integers? What about the cents?
def input_number_of_items():
"""Asks the user how many items he bought"""
number_of_items_raw = input("How many items do you have? ")
number_of_items = number_of_items_raw.replace(',', '') # Not quite sure what this is supposed to do
return int(number_of_items)
def input_item_sell_price():
"""Asks the user how much he can sell one item for"""
item_sell_price_raw = input("How much does one item sell for? ")
item_sell_price = item_sell_price_raw.replace(',', '') # Not quite sure what this is supposed to do
return int(item_sell_price)
def is_input_valid(total_cost, number_of_items, item_sell_price):
"""Checks if the user inputs are valid"""
# TODO: Split each check into new smaller functions if they get too big. Now it's just one if, so it's ok.
input_valid = True
# Check total cost
# Nothing to check
# Check number of items
if number_of_items < 1:
print("You have to buy at least one item!")
input_valid = False
# Check item sell price
if item_sell_price <= 0:
print("You can't sell your item for a negetive price!")
input_valid = False
# True if all checks passed
return input_valid
# - Business Logic Functions -
def calculate_total_sell_price(number_of_items, item_price):
"""Calculates how much you could sell all your items for"""
return number_of_items * item_price
def calculate_total_sell_price_without_tax(number_of_items, item_price):
"""Calculates how much money you have after you subtract the tax"""
price_with_tax = calculate_total_sell_price(number_of_items, item_price)
remaining_ratio = (1.0 - SALES_TAX/100.0)
price_without_tax = remaining_ratio * price_with_tax
return price_without_tax
def evaluate_profit(cost, revenue):
"""Evaluates if your investment would be profitable, and prints an according text"""
profit = revenue - cost
if not profit > 0:
loss = -profit
print("It's not worth it to make this investment. I would lose {0:,.2f} {1}".format(loss, CURRENCY_UNIT))
else:
print("I would make this investment, because I would earn {0:,.2f} {1}".format(profit, CURRENCY_UNIT))
# - Main Function -
def main():
"""The main program that is composed of all the small functions"""
while True: # Repeat the inputs until they are valid
total_cost = input_total_cost()
number_of_items = input_number_of_items()
item_sell_price = input_item_sell_price()
if not is_input_valid(total_cost, number_of_items, item_sell_price):
print("Please retry.")
else:
break
revenue = calculate_total_sell_price_without_tax(number_of_items, item_sell_price)
evaluate_profit(total_cost, revenue)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment