BMI .HTML AND BMI.CGI

BMI .HTML AND BMI.CGI

<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<title>BMI Calculator</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.container {
max-width: 300px;
margin: auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
input {
margin: 10px 0;
width: 100%;
padding: 8px;
}
button {
padding: 10px;
width: 100%;
}
.result {
margin-top: 20px;
}
</style>
</head>
<body>

<div class=”container”>
<h2>BMI Calculator</h2>
<label for=”weight”>Weight (kg):</label>
<input type=”number” id=”weight” placeholder=”Enter weight” required>

<label for=”height”>Height (m):</label>
<input type=”number” id=”height” placeholder=”Enter height” required>

<button onclick=”calculateBMI()”>Calculate BMI</button>

<div class=”result” id=”result”></div>
</div>

<script>
function calculateBMI() {
const weight = parseFloat(document.getElementById(‘weight’).value);
const height = parseFloat(document.getElementById(‘height’).value);
const resultDiv = document.getElementById(‘result’);

if (isNaN(weight) || isNaN(height) || weight <= 0 || height <= 0) {
resultDiv.innerHTML = “Please enter valid positive numbers for weight and height.”;
return;
}

const bmi = weight / (height ** 2);
let category = ”;

if (bmi < 18.5) {
category = “Underweight”;
} else if (bmi < 24.9) {
category = “Normal weight”;
} else if (bmi < 29.9) {
category = “Overweight”;
} else {
category = “Obesity”;
}

resultDiv.innerHTML = `Your BMI is: ${bmi.toFixed(2)}<br>You are classified as: ${category}`;
}
</script>

</body>
</html>


#!/usr/bin/env python3

import cgi
import cgitb

# Enable debugging
cgitb.enable()

def calculate_bmi(weight, height):
return weight / (height ** 2)

def bmi_category(bmi):
if bmi < 18.5:
return “Underweight”
elif 18.5 <= bmi < 24.9:
return “Normal weight”
elif 25 <= bmi < 29.9:
return “Overweight”
else:
return “Obesity”

def main():
print(“Content-Type: text/html”) # HTML is following
print() # End of headers

# Get form data
form = cgi.FieldStorage()
try:
weight = float(form.getvalue(“weight”, 0))
height = float(form.getvalue(“height”, 0))
except (ValueError, TypeError):
print(“<h2>Error: Please enter valid numbers for weight and height.</h2>”)
return

# Calculate BMI and category
if weight <= 0 or height <= 0:
print(“<h2>Error: Weight and height must be positive numbers.</h2>”)
return

bmi = calculate_bmi(weight, height)
category = bmi_category(bmi)

# Generate HTML output
print(f”””
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<title>BMI Calculator Result</title>
<style>
body {{ font-family: Arial, sans-serif; margin: 20px; }}
.result {{ margin-top: 20px; }}
</style>
</head>
<body>
<h2>BMI Calculator Result</h2>
<p>Your BMI is: {bmi:.2f}</p>
<p>You are classified as: {category}</p>
<a href=”/cgi-bin/bmi_form.html”>Calculate again</a>
</body>
</html>
“””)

if __name__ == “__main__”:
main()

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です