Python 03

Python Functions 02

Convert old apps into functions

Let’s bring our old apps from the previous posts.

Tip Calculator App

food_amount = float(input("Enter Food Amount $: "))
tip_percentage = float(input("Enter your tip percentage %: ")) / 100
# how much am I paying in total?
tip_amount = food_amount * tip_percentage
total = food_amount + tip_amount
print('\n\n')
print('=================================')
print(f'πŸ— Food Amount: ${food_amount}')
print(f'πŸ’Έ Tip Amount: ${tip_amount}')
print('\n')
print(f'πŸ’° Total Amount: ${total}')
print('=================================')

Do you remember the tip calculator that we previously built? Let’s refactor this into a function.

Toggle answer πŸ‘‡
food = float(input("Enter Food Amount $: "))
tip_percentage = float(input("Enter your tip percentage %: "))

def calculateFoodTotal(food, tip_percentage):
  tip = food * (tip_percentage / 100)
  total = food + tip
  print('\n\n')
  print('=================================')
  print(f'πŸ— Food Amount: ${food}')
  print(f'πŸ’Έ Tip Amount: ${tip}')
  print('\n')
  print(f'πŸ’° Total Amount: ${total}')
  print('=================================')
  return total

calculateFoodTotal(food, tip_percentage)

Type hinting

Before we move on to the next app, let’s quickly skim through type hinting. Type hinting is essentially documenting code without β€˜β€™β€™.

'''
weather_to_emoji takes in 1 argument as a string
(expected inputs from users: 'rain', 'cloudy'...)
>>> weather_to_emoji('rain')
β˜”
'''

To be simple, it is programmatic documentation as you type the code.

Tiny Weather App

Take a look at the weather app that we created before,

weather = input('What is the weather like? : ')

if weather == 'rain':
  print('β˜”')
elif weather == 'cloudy':
  print('☁️')
elif weather == 'thunderstorm':
  print('⚑')
else:
  print('😎')

How do we transform this into a function with the type hinting? πŸ›‘ As the result print(), the return value is None.

weather = input('What is the weather like? : ')

def weather_to_emoji(weather: str) -> None:
  if weather == 'rain':
    print('β˜”')
  elif weather == 'cloudy':
    print('☁️')
  elif weather == 'thunderstorm':
    print('⚑')
  else:
    print('😎')

weather_to_emoji(weather)

Did you notice the difference?

def weather_to_emoji(weather: str) -> None:

In the parenthesis, we can put what type of data is expected for the parameter. Furthermore, -> None part is distinct that this function has a return value of None

Try typing str and None in your python console. Alt text

Type hinting 02

Let’s practice type hinting with the existing functions.

  • tiny sum

    Change the function sum with type hinting

def sum(a, b):
  '''
  Takes two integers and returns their sum
  '''
  return a + b

print(sum(1,3))

⬇️ ⬇️ ⬇️ ⬇️

'''
❌ we don't need this part anymore
Takes two integers and returns their sum
'''
def sum(a: int, b:int) -> int:
  return a + b

print(sum(1,3))
  • Baby tip calculator

    Change the function calculateFoodTotal with type hinting.

Toggle answer πŸ‘‡
food = float(input("Enter Food Amount $: "))
tip_percentage = float(input("Enter your tip percentage %: "))

def calculateFoodTotal(food: float, tip_percentage: int) -> float:
  tip = food * (tip_percentage / 100)
  total = food + tip
  print('\n\n')
  print('=================================')
  print(f'πŸ— Food Amount: ${food}')
  print(f'πŸ’Έ Tip Amount: ${tip}')
  print('\n')
  print(f'πŸ’° Total Amount: ${total}')
  print('=================================')
  return total

calculateFoodTotal(food, tip_percentage)

Exercises

Bigger guy

Make the function that takes two numbers and returns a bigger number.

def bigger_guy(a, b):
  """
  Given two numbers, return the bigger one.
  >>> bigger_guy(2, 3)
  3
  """

Ansers will vary

Toggle answer πŸ‘‡
def bigger_guy(a: int, b: int) -> int:
  if a > b:
    return a
  else:
    return b

Categories:

Updated:

Comments