# Python Basics Exercises: Functions and Loops (Summary)

Copied!
Happy Pythoning!

In this video course, you practiced what you learned in Python Basics: Functions and Loops. You created custom functions, which consist of two parts:

1. The function signature, initiated with the `def` keyword, which includes the function’s name and parameters.
2. The function body, where the code executes every time the function is called.

Functions provide reusable components in your code, preventing repetition and enhancing readability and maintenance.

Along the way, you practiced using `for` loops, taking input from the user, and formatting numbers in f-strings.

If you haven’t already tackled the Python Basics: Functions and Loops Quiz, now is a great time to give it a whirl.

For further insights into the concepts covered in this video course, you can refer to the following tutorials:

Or you can explore the following video courses:

To continue your Python learning journey, check out the other Python Basics courses. You might also consider getting yourself a copy of Python Basics: A Practical Introduction to Python 3.

Sample Code (.zip)

3.9 KB

Course Slides (.pdf)

7.7 MB

Anonymous

Spent a little bit too much on this, but learned a lot. Also gave myself some extra challenge by calculating the monthly interest and some small error handling. There are probably minor errors here and there:

``````import sys

#--------------------------- functions ----------------------------------------
def compound_interest_core(principal_amount, rate_per_period, period):
return principal_amount * (1 + rate_per_period) ** period

def annual_interest(amount, rate, periods):
for year in range(1, periods+1):
year_amount = compound_interest_core(amount, rate, year)
print(f'Year {year}: \${year_amount:,.2f}')

# output interest earned
print((f'Total interest earned after {periods} years with annual compounding: '
f'\${year_amount - amount:,.2f}'))

def monthly_interest(amount, rate, periods):
principal_amount = amount
months = periods * 12
monthly_rate = rate / 12

for month in range(1, months+1):
amount = compound_interest_core(amount, monthly_rate, 1)
if month % 12 == 0:
year = month // 12
print(f'End of Year {year}: \${amount:,.2f}')

# Output interest earned
print((f'Total interest earned after {periods} years with monthly compounding: '
f'\${amount - principal_amount:,.2f}'))
#------------------------- end of functions -----------------------------------

while True:
compounds = ('monthly', 'annually')

compound_period = input('Enter the compound period? monthly or annually: ').lower()
if not compound_period in compounds:
print('Aceptable inputs: monthly or annually. try again!\n')
continue

principal_amount = float(input('Principal Amount: '))
interest_rate = float(input('Interest Rate: '))
periods = int(input('Time: '))

if compound_period == 'monthly':
monthly_interest(principal_amount, interest_rate, periods)
elif compound_period == 'annually':
annual_interest(principal_amount, interest_rate, periods)

print()

while True:
choices_for_exit_prompt = ('y', 'n')
restart_calc = input('Restart? y/n: ')
if not restart_calc in choices_for_exit_prompt:
print('Acceptable input: y/n. try again!\n')
continue

if restart_calc == 'n': sys.exit('Exiting...')

print('Restarting...\n')
break
``````

rwelk

I to spent a lot of time on the investment challange. It forced me to review functions in the suggested resources courses. I added this line so I could use the integer 5 instead which seems to work would this be considered Pythonic?

rate = float(input(“Enter rate of interest: “))/100

John Akpan

This is good. To make things simpler, I will use the try and except ValueError in the code. It helps manages the exception codes and troubleshooting.

to join the conversation.