Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Created August 29, 2021 20:17
Show Gist options
  • Save vlad-bezden/718593ffc6036c8e7ac261343ee50a09 to your computer and use it in GitHub Desktop.
Save vlad-bezden/718593ffc6036c8e7ac261343ee50a09 to your computer and use it in GitHub Desktop.
Calculate and show caffeine level in body with time.
"""Calculate and show caffeine level in body with time."""
from math import exp
from typing import Iterable
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [24, 12]
# caffeine half life in hours
HALF_LIFE_HOURS = 5
def calculate(caffeines: int, time: int) -> Iterable[float]:
return [
round(caffeines, 2) * exp(-0.693 * hour / HALF_LIFE_HOURS)
for hour
in range(time + 1)
]
def plot(data: Iterable[float]) -> None:
plt.title("Caffeine level in body with time")
plt.xlabel("Hours)")
plt.ylabel("mg")
plt.plot(data, color="red")
plt.grid()
plt.show()
def main() -> None:
while True:
try:
level = int(input("Enter initial caffeine level in mg: "))
time = int(input("Enter time to calculate caffeine in body: "))
break
except ValueError:
print("Invalid value. Try again")
plot(calculate(level, time))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment