Ramadhan Python Challenge — Day 28


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:

  1. It defines a calculate_cfr function that takes the number of deaths and the number of confirmed cases as input.
  2. Inside this function, it checks if there are no confirmed cases (denominator is 0) and returns an error message in such cases.
  3. If there are confirmed cases, it calculates the CFR as (deaths / confirmed_cases) * 100.
  4. The main function prompts the user to enter the number of deaths and the number of confirmed cases.
  5. It then calls calculate_cfr with the provided numbers and prints the CFR.
  6. print: This is the Python built-in function used to display output to the console.
  7. "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.
    So, when you see {:.2f}%, it means “insert a floating-point number with two decimal places, followed by a ‘%'”.
  8. .format(cfr): This is where the formatting happens. The .format() method of the string takes the value of cfr 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, if cfr is 3.14159, then {:.2f}% will format it to 3.14%, and this formatted string will replace the placeholder in the original string.
  9. 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.

Tinggalkan komentar

Situs ini menggunakan Akismet untuk mengurangi spam. Pelajari bagaimana data komentar Anda diproses.