0% found this document useful (0 votes)
15 views12 pages

Python Programming

Uploaded by

susmithareddy959
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views12 pages

Python Programming

Uploaded by

susmithareddy959
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Python Programming

Project 1:BMI Calculator


Code:
def calculate_bmi(weight, height):
"""
Calculate BMI using the formula: weight (kg) /
(height (m) ^ 2)

Parameters:
weight (float): Weight in kilograms
height (float): Height in meters

Returns:
float: Calculated BMI
"""
bmi = weight / (height ** 2)
return bmi

def bmi_category(bmi):
"""
Determine the BMI category based on the BMI
value.

Parameters:
bmi (float): The calculated BMI

Returns:
str: The category of 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():
"""
Main function to execute the BMI calculator.
"""
try:
weight = float(input("Enter your weight in kg: "))
height = float(input("Enter your height in meters:
"))

bmi = calculate_bmi(weight, height)


category = bmi_category(bmi)

print(f"Your BMI is: {bmi:.2f}")


print(f"You are classified as: {category}")

except ValueError:
print("Please enter valid numerical values for
weight and height.")

if __name__ == "__main__":
main()
OUTPUT:
Enter your weight in kg: 52
Enter your height in meters: 1.64
Your BMI is: 19.33
You are classified as: Normal weight

=== Code Execution Successful


===

Project 2: SIMPLE PASSWORD


GENERATOR
CODE:
import random
import string

def generate_password(length, use_uppercase,


use_digits, use_special_chars):
"""
Generate a random password based on the
specified criteria.

Parameters:
length (int): Length of the password
use_uppercase (bool): Whether to include
uppercase letters
use_digits (bool): Whether to include digits
use_special_chars (bool): Whether to include
special characters

Returns:
str: Generated password
"""
# Base characters for the password
characters = string.ascii_lowercase # Always
include lowercase letters

if use_uppercase:
characters += string.ascii_uppercase
if use_digits:
characters += string.digits
if use_special_chars:
characters += string.punctuation

# Generate a random password


password = ''.join(random.choice(characters) for _
in range(length))
return password

def main():
print("Welcome to the Simple Password
Generator!")

try:
length = int(input("Enter the desired length of the
password: "))
use_uppercase = input("Include uppercase
letters? (y/n): ").lower() == 'y'
use_digits = input("Include digits? (y/n):
").lower() == 'y'
use_special_chars = input("Include special
characters? (y/n): ").lower() == 'y'

password = generate_password(length,
use_uppercase, use_digits, use_special_chars)
print(f"Generated Password: {password}")

except ValueError:
print("Please enter a valid number for the
length.")

if __name__ == "__main__":
main()

OUTPUT:
Welcome to the Simple Password Generator!
Enter the desired length of the password: 7
Include uppercase letters? (y/n): y
Include digits? (y/n): y
Include special characters? (y/n): y
Generated Password: ]S6K3|z

=== Code Execution Successful ===

PROJECT 3:Weather app with javascript

CODE:
Index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Weather App</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Weather App</h1>
<select id="citySelect">
<option value="">Select a city</option>
<option value="New York">New York</option>
<option value="London">London</option>
<option value="Tokyo">Tokyo</option>
<option value="Paris">Paris</option>
<option value="Sydney">Sydney</option>
</select>
<input type="text" id="cityInput" placeholder="Or
enter city name">
<button id="searchButton">Get Weather</button>
<div class="weather-info">
<h2 id="location"></h2>
<p id="temperature"></p>
<p id="description"></p>
</div>
</div>
<script src="script.js"></script>
</body>
</html>

Style.css:
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}

.container {
max-width: 400px;
margin: 0 auto;
text-align: center;
padding: 20px;
background-color: rgba(255, 255, 255, 0.8);
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

input[type="text"], select {
width: 100%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 5px;
}

button {
background-color: #007BFF;
color: #fff;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
}

.weather-info {
margin-top: 20px;
}
Script.jss:
const apiKey = 'YOUR_API_KEY'; // Replace with your
OpenWeatherMap API key
const apiUrl =
'https://api.openweathermap.org/data/2.5/weather';

document.getElementById('searchButton').addEventListener
('click', () => {
const cityInput =
document.getElementById('cityInput').value;
const citySelect =
document.getElementById('citySelect').value;
const city = cityInput || citySelect; // Use input or
selected city

if (city) {
fetchWeather(city);
}
});

function fetchWeather(city) {
const url = `${apiUrl}?q=${city}&appid=$
{apiKey}&units=metric`;
fetch(url)
.then(response => response.json())
.then(data => {
if (data.cod === 200) {
displayWeather(data);
} else {
alert('City not found. Please try again.');
}
})
.catch(error => {
console.error('Error fetching weather data:', error);
});
}

function displayWeather(data) {
document.getElementById('location').textContent = `$
{data.name}, ${data.sys.country}`;
document.getElementById('temperature').textContent =
`${Math.round(data.main.temp)}°C`;
document.getElementById('description').textContent =
data.weather[0].description;
}

You might also like