Programming 4
Programming 4
Course:
The task is to develop a function that calculates the length of the hypotenuse of a right triangle given the lengths of
the other two sides (legs). The formula for this calculation is derived from the Pythagorean theorem: \(c = \sqrt{a^2
+ b^2}\), where \(c\) is the length of the hypotenuse, and \(a\) and \(b\) are the lengths of the other two sides.
To perform mathematical calculations such as square root, we need to import Python's built-in `math` library. This
Screenshot.
Step # 2:
Screenshot.
Let's define a simple function called `calculate_hypotenuse` that takes the lengths of the two legs (`a` and `b`) as
arguments.
```
pass
```
Step 3:
Now, we'll add the Pythagorean theorem formula to the function using Python's `math.sqrt()` function to calculate
```
return hypotenuse
```
This function calculates the length of the hypotenuse given the lengths of the other two sides.
Screenshot
Screenshot.
Step 4:
Let's test the `calculate_hypotenuse` function with the provided arguments (3, 4), which represent the lengths of the
```
import math
return hypotenuse
Test the function
result = calculate_hypotenuse(3, 4)
```
This code will calculate and print the length of the hypotenuse for a right triangle with legs of length 3 and 4.
Screenshot
Output screenshot.
Additional calls.
Call 1 screenshot.
Screenshot.
Output screenshot.
Call 2
Screenshot.
Output screenshot
Part 2.
Step 1:
Importing Necessary Libraries
To create a random password generator, we'll use Python's built-in `random` and `string` libraries.
- `random`: Provides functionality for generating random numbers and selections, which we'll use to pick random
- `string`: Offers predefined constants like `ascii_letters`, `digits`, and `punctuation`, making it easy to define
```
import random
import string
```
Screenshot.
Step#2.
Screenshot.
Screenshot
Step 3:
We'll define different sets of characters that will be used to build the password. These sets include:
```
if include_symbols:
characters += string.punctuation
```
This code combines the character sets based on the `include_symbols` parameter, allowing us to generate
Screenshot.
Step 3: Defining Character Sets
```
letters = string.ascii_letters
digits = string.digits
```
This code sets up the character sets based on the `include_symbols` parameter, determining whether special
To generate a password with a mix of alphabets, numbers, and special symbols (if included), we'll combine the
character sets:
```
```
This combined character set will be used to randomly select characters and generate the password.
Screenshot.
Screenshot.
Step 5.
We'll check if the password length is valid. If the length is less than 1, we'll raise an error.
```
if length < 1:
```
This validation ensures that the password length is reasonable and prevents potential issues during password
generation.
Screenshot.
Step 6.
We'll use a loop to select random characters from the combined character
```
```
This code generates a password of the specified length by randomly choosing characters from the combined set of
Screenshot.
- `for _ in range(length)`: Ensures the password has the correct length by repeating the random selection process.
- `''.join(...)`: Concatenates the randomly selected characters into a single string, creating the final password.
Step 7.
```
def main():
while True:
try:
if length < 1:
else:
break
except ValueError:
- Asks the user for the password length and validates the input
- Prepares to call the password generation function with the user's input.
Screenshot.
The loop ensures the user enters a valid length by:
- Using a try-except block to catch and handle invalid inputs (like non-numeric values)
Step 8
We'll ask the user if they want to include special characters in their password:
```
This code:
- Sets `include_symbols` to `True` if the user enters "yes", and `False` otherwise.
Screenshot.
Step 9
Screenshot
Step 10.
Screenshot.
Three outputs of the function.
Screenshot.
Screenshot
Screenshot.
Reference.
Downey, A. (2015). Think Python: How to think like a computer scientist. Needham,
https://docS,pvthon.org/3Aibrary/random.htmlhttps:docs.pvthon.org31ibrary/string.htmlhttps:/
realpython.com'documenting-python-code