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
andNone
in your python console.
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
Comments