1

How do I show all the depreciation values added together at the end? Its only showing the one for the last year. Also if there is anything else I could add to my code I would appreciate any advice.

# Constants to use for deciding large or small apartment
LARGE = 0.10
SMALL = 0.08
# inputs for getting user information
try: #data validation
    cost = float(input("Enter how much the house is worth:\n "))
except:
    print("Please enter a number. Restart program")
    exit()
size = (input("How big is your apartment? (Large or Small):\n ")).upper()
years = int(input("How long is the schedule? (Years):\n "))
# Rate selection
if size == 'Large' or 'large':
    rate = LARGE
if size == 'Small' or 'small' :
    rate = SMALL

# Header for the table
print(f'\n{"Year":>10}{"Current value":>15}{"Depreciated value":>25}{"Depreciation":>15}')

# Calculate the results for each year and show the user
for year in range(1, years+1):
    value = cost * rate
    aptcost = cost - value
    print(f'{year:<4} {cost:>15.2f} {aptcost :25.2f} {value:15.2f}')
    cost = aptcost

    # Display the results to the user
print(f'\n\nDepreciation rate is: {rate:.2f}')
print(f'Total depreciation upon end of schedule: ${aptcost:.2f}')
vividdream
  • 11
  • 1
  • 1
    Unrelated but important: `size == 'Large' or 'large'` is always [truthy](https://stackoverflow.com/questions/39983695/what-is-truthy-and-falsy-how-is-it-different-from-true-and-false). – pasbi Apr 06 '20 at 06:39

2 Answers2

0

Not sure if this is what you want to do but the total depreciation is the cost entered by user minus aptcost at the end of the your for loop. So maybe something like this:

original_cost = cost
for year in range(1, years+1):
     value = cost * rate
     aptcost = cost - value
     print(f'{year:<4} {cost:>15.2f} {aptcost :25.2f} {value:15.2f}')
     cost = aptcost

# Display the results to the user
print(f'\n\nDepreciation rate is: {rate:.2f}')
print(f'Total depreciation upon end of schedule: ${original_cost-aptcost:.2f}')

Hope it helps!!

0
# Constants to use for deciding large or small apartment
LARGE = 0.10
SMALL = 0.08
# inputs for getting user information
'''
   Instead of restarting the entire program just loop until a valid value is entered
   Including all input into 1 loop is not ideal as an error will force the user to start from the beginning again, 
   you can split every input into its own loop to imporove the experience
'''
while True:
    try:
        cost = float(input("Enter how much the house is worth:\n "))
        size = (input("How big is your apartment? (Large or Small):\n ")).upper()
        if size not in ['LARGE','SMALL']:
            print("Please enter a valid size: Large or Small")
            continue
        years = int(input("How long is the schedule? (Years):\n "))
    except ValueError: # Call Value Error, don`t leave your except open
        print("Please enter a valid value")
    else:
        # Rate selection
        if size == 'LARGE': rate = LARGE
        else: rate = SMALL
        break # Break loop if no value validation errors occurred

# Header for the table
print(f'\n{"Year":>10}{"Current value":>15}{"Depreciated value":>25}{"Depreciation":>15}')

# Calculate the results for each year and show the user
initial_cost = cost
total_depreciation = 0
for year in range(years):
    depreciation_value = cost * rate
    depreciation_cost = cost - depreciation_value
    total_depreciation += depreciation_value
    print(f'{year+1:<4} {cost:>15.2f} {depreciation_cost :25.2f} {depreciation_value :15.2f}')
    cost = depreciation_cost

    # Display the results to the user
print(f'\n\nDepreciation rate is: {rate:.2f}')
print(f'Total depreciation upon end of schedule: ${total_depreciation:.2f}')
Lambo
  • 1,094
  • 11
  • 18