To push me to remember Python in Ramadhan 2024, then I will try to put one post per day.
Question
Create a simple program to calculate Case Fatality Rate (CFR).
Answer
def calculate_cfr(deaths, confirmed_cases):
"""
Calculate Case Fatality Rate (CFR).
deaths: Number of deaths.
confirmed_cases: Number of confirmed cases.
"""
if confirmed_cases == 0:
return "Cannot calculate CFR. No confirmed cases."
cfr = (deaths / confirmed_cases) * 100
return cfr
def main():
print("Case Fatality Rate (CFR) Calculator")
try:
deaths = int(input("Enter number of deaths: "))
confirmed_cases = int(input("Enter number of confirmed cases: "))
except ValueError:
print("Invalid input. Please enter a whole number.")
return
cfr = calculate_cfr(deaths, confirmed_cases)
if isinstance(cfr, str):
print(cfr)
else:
print("Case Fatality Rate (CFR): {:.2f}%".format(cfr))
if __name__ == "__main__":
main()
Explanation
The Case Fatality Rate (CFR) is calculated as the number of deaths divided by the number of confirmed cases.
Here’s how the script works:
- It defines a
calculate_cfr
function that takes the number of deaths and the number of confirmed cases as input. - Inside this function, it checks if there are no confirmed cases (denominator is 0) and returns an error message in such cases.
- If there are confirmed cases, it calculates the CFR as (deaths / confirmed_cases) * 100.
- The
main
function prompts the user to enter the number of deaths and the number of confirmed cases. - It then calls
calculate_cfr
with the provided numbers and prints the CFR. print
: This is the Python built-in function used to display output to the console."Case Fatality Rate (CFR): {:.2f}%"
: This is the string that will be printed. Let’s break down its components:"Case Fatality Rate (CFR):"
: This part is a fixed string that will be displayed as is.{:.2f}
: This is a placeholder for a floating-point number with two decimal places. Here’s what each part means::
: This marks the beginning of the format specifier..2
: This specifies that the number should be formatted with two decimal places.f
: This indicates that the placeholder should be replaced with a floating-point number.
%
: This is a literal “%” character, which will be displayed after the floating-point number.
{:.2f}%
, it means “insert a floating-point number with two decimal places, followed by a ‘%'”..format(cfr)
: This is where the formatting happens. The.format()
method of the string takes the value ofcfr
and substitutes it into the placeholder in the string. In this case,cfr
is a floating-point number (like 3.14), and it will be formatted to have two decimal places and displayed in place of{:.2f}
.For example, ifcfr
is 3.14159, then{:.2f}%
will format it to3.14%
, and this formatted string will replace the placeholder in the original string.- So, putting it all together: If
cfr
is 3.14159, then"Case Fatality Rate (CFR): {:.2f}%".format(cfr)
will become"Case Fatality Rate (CFR): 3.14%"
, which will be printed on the console. This formatting allows you to control how numbers are displayed in your output, ensuring they have the correct number of decimal places and any additional text you want to include.