My Python Project
My Python Project
To create a Python program that requests the user to input their name and password, and then
redirects them to select a photo of their choice, you can use the following code:
```python
Function to display the available photos and ask for user's choice
def select_photo():
if validate_credentials():
if select_photo():
print("Access granted!")
```
In this code, we first import the `getpass` module from the standard library to securely prompt
the user for their password without displaying it on the screen.
The `validate_credentials()` function prompts the user to enter their name and password. It then
checks if the provided credentials match the expected values (in this example, "username" and
"password"). If the credentials are correct, it returns `True`, indicating successful login. Otherwise,
it returns `False`.
The `select_photo()` function displays a list of available photos and asks the user to select their
favorite by entering the corresponding number. In this example, there are eight photos
represented by filenames. If the user selects photo number 8, they are granted access;
otherwise, access is denied.
Finally, in the main program execution, we call `validate_credentials()` to check if the user's login
is successful. If it is, we proceed to call `select_photo()`. If both functions return `True`, we print
"Access granted!".
correct
To implement the desired functionality in Python, you can use the following code:
```python
name = input("Enter your name: ")
password = input("Enter your password: ")
if choice == 3:
print("Congratulations! You have granted access.")
else:
print("Sorry, you did not choose the correct photo.")
else:
print("Invalid credentials. Access denied.")
```
In this code, we first prompt the user to enter their name and password using the `input()`
function. We then check if the entered name is either "alvin" or "nes" (case-insensitive) and if the
password matches "Daniel kariuki". If both conditions are true, we proceed to display the 8
photos and ask the user to choose their favorite by entering the corresponding number.
If the user selects photo number 3, they are granted access and a success message is displayed.
Otherwise, if they choose any other photo, a failure message is displayed.
Please note that this code only provides a basic implementation for demonstration purposes. In
a real-world scenario, you would need to consider additional security measures and validation
checks.