My 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
Importing necessary libraries
import getpass
Function to validate the user's credentials
def validate_credentials():
Requesting user input for name and password
name = input("Enter your name: ")
password = getpass.getpass("Enter your password: ")
Checking if the credentials are correct
if name == "username" and password == "password":
print("Login successful!")
return True
else:
print("Invalid credentials. Please try again.")
return False
Function to display the available photos and ask for user's choice
def select_photo():
List of available photos
photos = ["photo1.jpg", "photo2.jpg", "photo3.jpg", "photo4.jpg", "photo5.jpg", "photo6.jpg",
"photo7.jpg", "photo8.jpg"]
Displaying the photos
print("Please select your favorite photo:")
for i, photo in enumerate(photos):
print(f"{i+1}. {photo}")
Requesting user input for photo choice
choice = int(input("Enter the number of your favorite photo: "))
Validating the user's choice
if choice == 8:
print("Congratulations! You have chosen the correct photo.")
return True
else:
print("Sorry, wrong choice. Access denied.")
return False
Main program execution
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 name.lower() in ['alvin', 'nes'] and password == 'Daniel kariuki':
print("Welcome! Please choose your favorite photo.")
Display the 8 photos
print("1. Photo 1")
print("2. Photo 2")
print("3. Photo 3")
print("4. Photo 4")
print("5. Photo 5")
print("6. Photo 6")
print("7. Photo 7")
print("8. Photo 8")
choice = int(input("Enter the number of your favorite photo: "))
if choice == 3:
print("Congratulations! You have granted access.")
Redirect to the desired location or perform further actions
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.
Top 3 Authoritative Reference Publications or Domain Names:
1. Python Documentation - https://docs.python.org/
2. W3Schools - https://www.w3schools.com/python/
3. GeeksforGeeks - https://www.geeksforgeeks.org/